Skip to content

Commit 6b9fd16

Browse files
committed
v1.1.3 Color Fix and Folder Fix
- Properly saves color settings for cars so you don't have to redo color settings every time you open the game. - No longer throws errors when you place cars in a folder named "Assets" - Newtonsoft.json is no longer in use. Now using JsonFx
1 parent 051308d commit 6b9fd16

8 files changed

Lines changed: 115 additions & 72 deletions

File tree

Distance.CustomCar/Data/Car/CarBuilder.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ private Dictionary<string, GameObject> LoadAssetsBundles()
135135
}
136136
}
137137

138-
try
138+
/*try
139139
{
140140
foreach (FileInfo assetsFile in assetsDirectory.GetFiles("*", SearchOption.AllDirectories).Concat(globalCarsDirectory.GetFiles("*", SearchOption.AllDirectories)).OrderBy(x => x.Name))
141141
{
@@ -178,9 +178,9 @@ private Dictionary<string, GameObject> LoadAssetsBundles()
178178
catch(Exception ex)
179179
{
180180
Mod.Log.LogWarning("No Assets folder in the custom car folder.");
181-
}
181+
}*/
182182

183-
try
183+
/*try
184184
{
185185
DirectoryInfo otherAssetsDirectory = new DirectoryInfo(Path.Combine(Directory.GetParent(Directory.GetParent(Path.GetDirectoryName(System.Reflection.Assembly.GetCallingAssembly().Location)).ToString()).ToString(), "Assets"));
186186
@@ -224,7 +224,7 @@ private Dictionary<string, GameObject> LoadAssetsBundles()
224224
catch (Exception ex)
225225
{
226226
Mod.Log.LogWarning($"No Assets folder in toplevel directory.");
227-
}
227+
}*/
228228

229229
DirectoryInfo profileDirectory = new DirectoryInfo(Directory.GetParent(Path.GetDirectoryName(System.Reflection.Assembly.GetCallingAssembly().Location)).ToString());
230230

Distance.CustomCar/Distance.CustomCar.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
<Reference Include="BepInEx">
4343
<HintPath>..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\Distance\BepInEx\core\BepInEx.dll</HintPath>
4444
</Reference>
45+
<Reference Include="JsonFx, Version=2.0.1209.2802, Culture=neutral, PublicKeyToken=315052dd637f8a52, processorArchitecture=MSIL">
46+
<HintPath>..\packages\JsonFx.2.0.1209.2802\lib\net35\JsonFx.dll</HintPath>
47+
</Reference>
4548
<Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, processorArchitecture=MSIL">
4649
<HintPath>..\packages\Json.Net.Unity3D.9.0.1\lib\net35\Newtonsoft.Json.dll</HintPath>
4750
</Reference>
@@ -65,6 +68,7 @@
6568
<Compile Include="Data\Materials\MaterialPropertyExport.cs" />
6669
<Compile Include="Data\Materials\MaterialPropertyInfo.cs" />
6770
<Compile Include="Data\Materials\PropertyType.cs" />
71+
<Compile Include="Extensions\System\Collections\Generic\DictionaryExtensions.cs" />
6872
<Compile Include="Extensions\UnityEngine\UnityEngine\GameObject.cs" />
6973
<Compile Include="FileSystem.cs" />
7074
<Compile Include="MessageBox.cs" />
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#pragma warning disable RCS1110
2+
using System.Collections.Generic;
3+
4+
public static class DictionaryExtensions
5+
{
6+
public static bool ContainsKey<T>(this Dictionary<string, object> obj, string key)
7+
{
8+
try
9+
{
10+
obj.GetItem<T>(key);
11+
return true;
12+
}
13+
catch
14+
{
15+
return false;
16+
}
17+
}
18+
19+
public static T GetItem<T>(this Dictionary<string, object> obj, string key)
20+
{
21+
if (!obj.ContainsKey(key))
22+
{
23+
throw new KeyNotFoundException($"The key requested doesn't exist in store: '{key}'.");
24+
}
25+
26+
try
27+
{
28+
return (T)System.Convert.ChangeType(obj[key], typeof(T));
29+
}
30+
catch (System.Exception e)
31+
{
32+
throw new System.Exception($"Failed type conversion exception has been thrown.", e);
33+
}
34+
}
35+
36+
public static T GetOrCreate<T>(this Dictionary<string, object> obj, string key) where T : new()
37+
{
38+
if (!obj.ContainsKey(key))
39+
{
40+
obj[key] = new T();
41+
}
42+
43+
return obj.GetItem<T>(key);
44+
}
45+
46+
public static T GetOrCreate<T>(this Dictionary<string, object> obj, string key, T defaultValue)
47+
{
48+
if (!obj.ContainsKey<T>(key))
49+
{
50+
obj[key] = defaultValue;
51+
}
52+
53+
return obj.GetItem<T>(key);
54+
}
55+
}

Distance.CustomCar/Plugin.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public sealed class Mod : BaseUnityPlugin
1515
//Mod Details
1616
private const string modGUID = "Distance.CustomCar";
1717
private const string modName = "Custom Car";
18-
private const string modVersion = "1.1.2";
18+
private const string modVersion = "1.1.3";
1919

2020
//Config Entry Strings
2121
public static string UseTrumpetKey = "Use Trumpet Horn";

Distance.CustomCar/ProfileCarColors.cs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,21 @@ protected void Awake()
2121
Save();
2222
}
2323

24-
protected Section Profile(string profileName)
24+
protected Dictionary<string, object> Profile(string profileName)
2525
{
26-
return Config.GetOrCreate(profileName, new Section());
26+
Mod.Log.LogInfo("Assigning Profile Name...");
27+
return Config.GetOrCreate(profileName, new Dictionary<string, object>());
2728
}
2829

29-
protected Section Vehicle(string profileName, string vehicleName)
30+
protected Dictionary<string, object> Vehicle(string profileName, string vehicleName)
3031
{
31-
return Profile(profileName).GetOrCreate(vehicleName, new Section());
32+
Mod.Log.LogInfo("Assigning Vehicle Name...");
33+
return Profile(profileName).GetOrCreate(vehicleName, new Dictionary<string, object>());
3234
}
3335

3436
protected CarColors GetCarColors(string profileName, string vehicleName)
3537
{
36-
Section vehicle = Vehicle(profileName, vehicleName);
38+
Dictionary<string, object> vehicle = Vehicle(profileName, vehicleName);
3739
CarColors colors = new CarColors
3840
{
3941
primary_ = GetColor(vehicle, "primary", Colors.whiteSmoke),
@@ -45,28 +47,29 @@ protected CarColors GetCarColors(string profileName, string vehicleName)
4547
return colors;
4648
}
4749

48-
protected Color GetColor(Section vehicle, string category, Color defaultColor)
50+
protected Color GetColor(Dictionary<string, object> vehicle, string category, Color defaultColor)
4951
{
50-
Section color = vehicle.GetOrCreate(category, new Section());
52+
//Mod.Log.LogWarning(e);
53+
Dictionary<string, object> color = vehicle.GetOrCreate(category, new Dictionary<string, object>());
5154

52-
var r = color.GetOrCreate("r", defaultColor.r);
53-
var g = color.GetOrCreate("g", defaultColor.g);
54-
var b = color.GetOrCreate("b", defaultColor.b);
55-
var a = color.GetOrCreate("a", defaultColor.a);
55+
float r = color.GetOrCreate("r", defaultColor.r);
56+
float g = color.GetOrCreate("g", defaultColor.g);
57+
float b = color.GetOrCreate("b", defaultColor.b);
58+
float a = color.GetOrCreate("a", defaultColor.a);
5659

57-
return new Color(r, g, b, a);
60+
return new Color(r, g, b, a);
5861
}
5962

6063
protected void SetCarColors(string profileName, string vehicleName, CarColors colors)
6164
{
62-
Section vehicle = Vehicle(profileName, vehicleName);
65+
Dictionary<string, object> vehicle = Vehicle(profileName, vehicleName);
6366

6467
vehicle["primary"] = ToSection(colors.primary_);
6568
vehicle["secondary"] = ToSection(colors.secondary_);
6669
vehicle["glow"] = ToSection(colors.glow_);
6770
vehicle["sparkle"] = ToSection(colors.sparkle_);
6871

69-
Section profile = Profile(profileName);
72+
Dictionary<string, object> profile = Profile(profileName);
7073
profile[vehicleName] = vehicle;
7174

7275
Config[profileName] = profile;
@@ -108,7 +111,9 @@ public void LoadAll()
108111
}
109112

110113
CarInfo carInfo = profileManager.CarInfos_[carIndex];
114+
Mod.Log.LogInfo($"Getting car color in {currentProfile.FileName_}'s profile for the {carInfo.name_} car");
111115
CarColors colors = GetCarColors(currentProfile.FileName_, carInfo.name_);
116+
112117

113118
carColors[carIndex] = colors;
114119
}

Distance.CustomCar/Section.cs

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
using Newtonsoft.Json;
2-
using Newtonsoft.Json.Linq;
3-
using System;
1+
using System;
42
using System.Collections.Generic;
53

64
namespace Distance.CustomCar
75
{
86
public class Section : Dictionary<string, object>
97
{
10-
public bool Dirty { get; protected set; }
8+
119
public event EventHandler<SettingsChangedEventArgs> ValueChanged;
1210

1311
public new object this[string key]
@@ -34,42 +32,26 @@ public class Section : Dictionary<string, object>
3432
base[key] = value;
3533
ValueChanged?.Invoke(this, new SettingsChangedEventArgs(key, oldValue, base[key]));
3634
}
37-
38-
Dirty = true;
3935
}
4036
}
4137

4238
public T GetItem<T>(string key)
4339
{
4440
if (!ContainsKey(key))
4541
{
46-
Mod.Log.LogInfo($"The key requested doesn't exist in store: '{key}'.");
42+
Mod.Log.LogError($"The key requested doesn't exist in store: '{key}'.");
4743
throw new KeyNotFoundException($"The key requested doesn't exist in store: '{key}'.");
4844
}
4945

46+
5047
try
5148
{
52-
if (this[key] is JToken jToken)
53-
return jToken.ToObject<T>();
54-
5549
return (T)Convert.ChangeType(this[key], typeof(T));
5650
}
57-
catch (JsonException je)
58-
{
59-
Mod.Log.LogInfo($"Failed to convert a JSON token to the requested type. String: {key} \n{je}");
60-
throw new SettingsException("Failed to convert a JSON token to the requested type.", key, true, je);
61-
}
6251
catch (Exception e)
6352
{
64-
try
65-
{
66-
return (T)Enum.ToObject(typeof(T), (long)this[key]);
67-
}
68-
catch
69-
{
70-
Mod.Log.LogInfo($".NET type conversion exception has been thrown. String: {key} \n{e}");
71-
throw new SettingsException($".NET type conversion exception has been thrown.", key, false, e);
72-
}
53+
Mod.Log.LogWarning($"Failed type conversion exception has been thrown. String: {key} \n{e}");
54+
throw new SettingsException($"Failed type conversion exception has been thrown.", key, false, e);
7355
}
7456
}
7557

@@ -86,12 +68,14 @@ public T GetItem<T>(string key)
8668

8769
public T GetOrCreate<T>(string key, T defaultValue)
8870
{
89-
if (!ContainsKey(key))
71+
if (!ContainsKey<T>(key))
9072
{
9173
this[key] = defaultValue;
9274
ValueChanged?.Invoke(this, new SettingsChangedEventArgs(key, null, this[key]));
9375
}
9476

77+
//Mod.Log.LogInfo(T.GetType());
78+
9579
return GetItem<T>(key);
9680
}
9781

Distance.CustomCar/Settings.cs

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
using Newtonsoft.Json;
2-
using Formatting = Newtonsoft.Json.Formatting;
1+
using JsonFx.Json;
2+
using JsonFx.Serialization;
33
using System;
44
using System.IO;
55
using System.Reflection;
@@ -25,65 +25,59 @@ public Settings(string fileName)
2525

2626
if (File.Exists(FilePath))
2727
{
28+
bool saveLater = false;
29+
2830
using (var sr = new StreamReader(FilePath))
2931
{
30-
var json = sr.ReadToEnd();
32+
string json = sr.ReadToEnd();
33+
JsonReader reader = new JsonReader();
3134
Section sec = null;
3235

3336
try
3437
{
35-
sec = JsonConvert.DeserializeObject<Section>(json);
36-
}
37-
catch (JsonException je)
38-
{
39-
Mod.Log.LogInfo(je);
38+
sec = reader.Read<Section>(json);
4039
}
4140
catch (Exception e)
4241
{
43-
Mod.Log.LogInfo(e);
42+
Mod.Log.LogWarning(e);
43+
saveLater = true;
4444
}
4545

46-
Mod.Log.LogInfo("Just Deserialized the json");
47-
4846
if (sec != null)
4947
{
50-
foreach (var k in sec.Keys)
48+
foreach (string k in sec.Keys)
49+
{
5150
Add(k, sec[k]);
51+
}
5252
}
5353
}
54-
}
5554

56-
Dirty = false;
55+
if (saveLater)
56+
{
57+
Save();
58+
}
59+
}
5760
}
5861

5962
public void Save(bool formatJson = true)
6063
{
6164
if (!Directory.Exists(SettingsDirectory))
6265
Directory.CreateDirectory(SettingsDirectory);
6366

67+
DataWriterSettings st = new DataWriterSettings { PrettyPrint = formatJson };
68+
JsonWriter writer = new JsonWriter(st);
69+
6470
try
6571
{
6672
using (var sw = new StreamWriter(FilePath, false))
6773
{
68-
sw.WriteLine(JsonConvert.SerializeObject(this, Formatting.Indented));
74+
sw.WriteLine(writer.Write(this));
6975
}
70-
71-
Dirty = false;
72-
}
73-
catch (JsonException je)
74-
{
75-
Mod.Log.LogInfo(je);
7676
}
7777
catch (Exception e)
7878
{
79-
Mod.Log.LogInfo(e);
79+
Mod.Log.LogWarning(e);
8080
}
8181
}
82-
83-
public void SaveIfDirty(bool formatJson = true)
84-
{
85-
if (Dirty)
86-
Save(formatJson);
87-
}
8882
}
8983
}

Distance.CustomCar/packages.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
33
<package id="Json.Net.Unity3D" version="9.0.1" targetFramework="net35" />
4+
<package id="JsonFx" version="2.0.1209.2802" targetFramework="net35" />
45
</packages>

0 commit comments

Comments
 (0)