generated from derail-valley-modding/template-umm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8209400
commit 4734bbe
Showing
8 changed files
with
298 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<AssemblyName>$(MSBuildProjectName)</AssemblyName> | ||
<TargetFramework>netframework4.8</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<LangVersion>8</LangVersion> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\CL.Common\CL.Common.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Reference Include="System.IO.Compression" /> | ||
<Reference Include="UnityEngine" /> | ||
<Reference Include="UnityEngine.AssetBundleModule" /> | ||
<Reference Include="UnityEngine.CoreModule" /> | ||
<Reference Include="UnityEngine.IMGUIModule" /> | ||
<Reference Include="UnityEngine.PhysicsModule" /> | ||
<Reference Include="UnityEditor" /> | ||
<Reference Include="DVLangHelper.Data" /> | ||
<Reference Include="DVLangHelper.Runtime" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using CL.Common; | ||
using Newtonsoft.Json; | ||
using System.IO; | ||
using UnityEditor; | ||
using UnityEngine; | ||
|
||
namespace CL.Unity | ||
{ | ||
[CreateAssetMenu(menuName = "DVCustomLicenses/Custom License")] | ||
internal class CustomLicenseContainer : ScriptableObject | ||
{ | ||
public CustomLicense License = new CustomLicense(); | ||
|
||
public string GetFullPath() | ||
{ | ||
string path = Application.dataPath; | ||
string assetPath = AssetDatabase.GetAssetPath(this); | ||
path = path + "/" + assetPath.Substring(7); | ||
return Path.GetDirectoryName(path); | ||
} | ||
|
||
public void Export() | ||
{ | ||
// Use a JsonSerializer for a cleaner output. | ||
JsonSerializer serializer = new JsonSerializer { Formatting = Formatting.Indented }; | ||
var path = $"{GetFullPath()}\\license.json"; | ||
|
||
using var file = File.CreateText(path); | ||
using var jsonWr = new JsonTextWriter(file); | ||
serializer.Serialize(jsonWr, License); | ||
|
||
EditorUtility.RevealInFinder(path); | ||
AssetDatabase.Refresh(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using UnityEditor; | ||
using UnityEngine; | ||
|
||
using static CL.Common.CustomLicense; | ||
|
||
namespace CL.Unity.Inspector | ||
{ | ||
[CustomPropertyDrawer(typeof(Colour))] | ||
internal class ColourDrawer : PropertyDrawer | ||
{ | ||
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) | ||
{ | ||
var r = property.FindPropertyRelative(nameof(Colour.R)); | ||
var g = property.FindPropertyRelative(nameof(Colour.G)); | ||
var b = property.FindPropertyRelative(nameof(Colour.B)); | ||
var a = property.FindPropertyRelative(nameof(Colour.A)); | ||
|
||
EditorGUI.BeginProperty(position, label, property); | ||
|
||
var result = EditorGUI.ColorField(position, label, new Color(r.floatValue, g.floatValue, b.floatValue, a.floatValue)); | ||
|
||
r.floatValue = result.r; | ||
g.floatValue = result.g; | ||
b.floatValue = result.b; | ||
a.floatValue = result.a; | ||
|
||
EditorGUI.EndProperty(); | ||
} | ||
|
||
public override float GetPropertyHeight(SerializedProperty property, GUIContent label) | ||
{ | ||
return EditorGUIUtility.singleLineHeight; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
using CL.Common; | ||
using DVLangHelper.Data; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using UnityEditor; | ||
using UnityEngine; | ||
|
||
using static CL.Common.CustomLicense; | ||
|
||
namespace CL.Unity.Inspector | ||
{ | ||
[CustomEditor(typeof(CustomLicenseContainer))] | ||
internal class CustomLicenseContainerEditor : Editor | ||
{ | ||
private static Dictionary<LicenseColour, Color32> LicenseToColour = new Dictionary<LicenseColour, Color32>() | ||
{ | ||
{ LicenseColour.Generic, new Color32( 43, 166, 166, byte.MaxValue) }, | ||
{ LicenseColour.Locomotive, new Color32( 83, 83, 83, byte.MaxValue) }, | ||
{ LicenseColour.Concurrent, new Color32(115, 86, 146, byte.MaxValue) }, | ||
{ LicenseColour.Hazmat, new Color32(166, 43, 43, byte.MaxValue) }, | ||
{ LicenseColour.Military, new Color32(126, 152, 95, byte.MaxValue) }, | ||
{ LicenseColour.Length, new Color32(146, 98, 59, byte.MaxValue) } | ||
}; | ||
|
||
private CustomLicenseContainer _container = null!; | ||
private SerializedProperty _license = null!; | ||
private LicenseColour _selectedColour; | ||
private FillType _selectedFill; | ||
|
||
private void OnEnable() | ||
{ | ||
_license = serializedObject.FindProperty(nameof(CustomLicenseContainer.License)); | ||
} | ||
|
||
public override void OnInspectorGUI() | ||
{ | ||
var current = _license.FindPropertyRelative(nameof(CustomLicense.LicenseType)); | ||
|
||
do | ||
{ | ||
EditorGUILayout.PropertyField(current); | ||
} while (current.Next(false)); | ||
|
||
_container = (CustomLicenseContainer)target; | ||
bool dirty = false; | ||
|
||
EditorGUILayout.Space(); | ||
|
||
using (new EditorGUILayout.HorizontalScope()) | ||
{ | ||
_selectedColour = (LicenseColour)EditorGUILayout.EnumPopup(_selectedColour); | ||
|
||
if (GUILayout.Button(new GUIContent("Set colour", | ||
"Sets the colour to one of the base game colours"), | ||
GUILayout.Width(EditorGUIUtility.currentViewWidth * 0.45f))) | ||
{ | ||
_container.License.Color = Colour.FromUnity(LicenseToColour[_selectedColour]); | ||
dirty = true; | ||
} | ||
} | ||
|
||
using (new EditorGUILayout.HorizontalScope()) | ||
{ | ||
_selectedFill = (FillType)EditorGUILayout.EnumPopup(_selectedFill); | ||
|
||
if (GUILayout.Button(new GUIContent("Autofill", | ||
"Auto fills english translations based on the name and type\n" + | ||
"Will not replace already written text"), | ||
GUILayout.Width(EditorGUIUtility.currentViewWidth * 0.45f))) | ||
{ | ||
Autofill(_container.License, _selectedFill); | ||
dirty = true; | ||
} | ||
} | ||
|
||
if (dirty) | ||
{ | ||
EditorUtility.SetDirty(target); | ||
AssetDatabase.SaveAssets(); | ||
serializedObject.Update(); | ||
} | ||
|
||
if (GUILayout.Button("Export")) | ||
{ | ||
_container.Export(); | ||
} | ||
} | ||
|
||
private static void Autofill(CustomLicense license, FillType fillType) | ||
{ | ||
var translation = license.TranslationName!.Items.Where(x => x.Language == DVLanguage.English).FirstOrDefault(); | ||
|
||
if (translation == null || string.IsNullOrEmpty(translation.Value)) | ||
{ | ||
Debug.LogWarning("No english name has been assigned to the license, cannot autofill!"); | ||
return; | ||
} | ||
|
||
var name = translation.Value; | ||
|
||
// Item name. | ||
var itemName = license.TranslationItem!.Items.Where(x => x.Language == DVLanguage.English).FirstOrDefault(); | ||
itemName ??= new TranslationItem(DVLanguage.English, string.Empty); | ||
|
||
if (string.IsNullOrEmpty(itemName.Value)) | ||
{ | ||
itemName.Value = $"License {name}"; | ||
} | ||
|
||
// Info item name. | ||
var infoItemName = license.TranslationInfoItem!.Items.Where(x => x.Language == DVLanguage.English).FirstOrDefault(); | ||
infoItemName ??= new TranslationItem(DVLanguage.English, string.Empty); | ||
|
||
if (string.IsNullOrEmpty(infoItemName.Value)) | ||
{ | ||
infoItemName.Value = $"License {name} Info"; | ||
} | ||
|
||
// Description. | ||
var description = license.TranslationDescription!.Items.Where(x => x.Language == DVLanguage.English).FirstOrDefault(); | ||
description ??= new TranslationItem(DVLanguage.English, string.Empty); | ||
|
||
if (string.IsNullOrEmpty(description.Value)) | ||
{ | ||
description.Value = fillType switch | ||
{ | ||
FillType.SteamLocomotive => "This license grants you access to all the [WHYTE WHEEL ARRANGEMENT] steam-powered locomotives in Derail Valley. " + | ||
"Use them responsibly! Refer to your vehicle catalog for detailed specifications.", | ||
FillType.DELocomotive => "This license grants you access to all the [# AXLES]-axle diesel-electric locomotives in Derail Valley. " + | ||
"Use them responsibly! Refer to your vehicle catalog for detailed specifications.", | ||
FillType.DHLocomotive => "This license grants you access to all the [# AXLES]-axle diesel-hydraulic locomotives in Derail Valley. " + | ||
"Use them responsibly! Refer to your vehicle catalog for detailed specifications.", | ||
FillType.DMLocomotive => "This license grants you access to all the [# AXLES]-axle diesel-mechanical locomotives in Derail Valley. " + | ||
"Use them responsibly! Refer to your vehicle catalog for detailed specifications.", | ||
FillType.JobType => $"This license grants you access to all orders of the {name} type. This order type involves [DESCRIPTION].", | ||
_ => string.Empty | ||
}; | ||
|
||
if (!string.IsNullOrEmpty(description.Value)) | ||
{ | ||
Debug.Log("Please fill out the fields marked [LIKE THIS] in the description of the license!"); | ||
} | ||
} | ||
} | ||
|
||
private enum LicenseColour | ||
{ | ||
Generic, | ||
Locomotive, | ||
Concurrent, | ||
Hazmat, | ||
Military, | ||
Length | ||
} | ||
|
||
private enum FillType | ||
{ | ||
NoDescription, | ||
SteamLocomotive, | ||
DELocomotive, | ||
DHLocomotive, | ||
DMLocomotive, | ||
JobType | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters