Skip to content

Commit 16d6e1c

Browse files
committed
BepInEx Ver
- Now on BepInEx! Yeah!
1 parent d90966d commit 16d6e1c

24 files changed

+855
-251
lines changed

Distance.CustomCar.Content/Distance.CustomCar.Content.projitems

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@
88
<PropertyGroup Label="Configuration">
99
<Import_RootNamespace>Distance.CustomCar.Content</Import_RootNamespace>
1010
</PropertyGroup>
11-
<ItemGroup>
12-
<Content Include="$(MSBuildThisFileDirectory)Mod\mod.json">
13-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
14-
</Content>
15-
</ItemGroup>
1611
<ItemGroup>
1712
<None Include="$(MSBuildThisFileDirectory)Mod\Assets\Backgrounds\Corruptor" />
1813
<None Include="$(MSBuildThisFileDirectory)Mod\Assets\Nitronic Rush\Avenger" />

Distance.CustomCar.Content/Mod/mod.json

Lines changed: 0 additions & 11 deletions
This file was deleted.

Distance.CustomCar/Assets.cs

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
using System;
2+
using System.IO;
3+
using System.Linq;
4+
using System.Reflection;
5+
6+
namespace Distance.CustomCar
7+
{
8+
public class Assets
9+
{
10+
private string _filePath = null;
11+
12+
private string RootDirectory { get; }
13+
private string FileName { get; set; }
14+
private string FilePath => _filePath ?? Path.Combine(Path.Combine(RootDirectory, "Assets"), FileName);
15+
16+
public object Bundle { get; private set; }
17+
18+
private Assets() { }
19+
20+
/// <summary>
21+
/// Attempts to construct a Unity AssetBundle via a Centrifuge Type Bridge.
22+
/// You will have to cast the Bundle property to Unity's AssetBundle type for usage.
23+
/// </summary>
24+
/// <param name="fileName">Filename/path relative to mod's private asset directory.</param>
25+
public Assets(string fileName)
26+
{
27+
RootDirectory = Path.GetDirectoryName(Assembly.GetCallingAssembly().Location);
28+
FileName = fileName;
29+
30+
if (!File.Exists(FilePath))
31+
{
32+
Mod.Log.LogInfo($"Couldn't find requested asset bundle at {FilePath}");
33+
return;
34+
}
35+
36+
Bundle = Load();
37+
}
38+
39+
/// <summary>
40+
/// Attempts to construct a Unity AssetBundle via a Centrifuge Type Bridge.
41+
/// You will have to cast the Bundle property to Unity's AssetBundle type for usage.
42+
/// </summary>
43+
/// <param name="filePath">An absolute path to the AssetBundle</param>
44+
public static Assets FromUnsafePath(string filePath)
45+
{
46+
if (!File.Exists(filePath))
47+
{
48+
Mod.Log.LogInfo($"Could not find requested asset bundle at {filePath}");
49+
return null;
50+
}
51+
52+
var ret = new Assets
53+
{
54+
_filePath = filePath,
55+
FileName = Path.GetFileName(filePath)
56+
};
57+
ret.Bundle = ret.Load();
58+
59+
if (ret.Bundle == null)
60+
return null;
61+
62+
return ret;
63+
}
64+
65+
private object Load()
66+
{
67+
try
68+
{
69+
var assetBundle = AssetBundleBridge.LoadFrom(FilePath);
70+
Mod.Log.LogInfo($"Loaded asset bundle {FilePath}");
71+
72+
return assetBundle;
73+
}
74+
catch (Exception ex)
75+
{
76+
Mod.Log.LogInfo(ex);
77+
return null;
78+
}
79+
}
80+
}
81+
82+
internal static class AssetBundleBridge
83+
{
84+
public static Type AssetBundleType => Kernel.FindTypeByFullName(
85+
"UnityEngine.AssetBundle",
86+
"UnityEngine"
87+
);
88+
89+
private static MethodInfo LoadFromFile => AssetBundleType.GetMethod(
90+
"LoadFromFile",
91+
new[] { typeof(string) }
92+
);
93+
94+
public static object LoadFrom(string path)
95+
{
96+
return LoadFromFile.Invoke(null, new[] { path });
97+
}
98+
}
99+
100+
internal static class Kernel
101+
{
102+
internal static Type FindTypeByFullName(string fullName, string assemblyFilter)
103+
{
104+
var assemblies = AppDomain.CurrentDomain.GetAssemblies()
105+
.Where(a => a.GetName().Name.Contains(assemblyFilter));
106+
107+
foreach (var asm in assemblies)
108+
{
109+
var type = asm.GetTypes().FirstOrDefault(t => t.FullName == fullName);
110+
111+
if (type == null)
112+
continue;
113+
114+
return type;
115+
}
116+
117+
Mod.Log.LogInfo($"Type {fullName} wasn't found in the main AppDomain at this moment.");
118+
throw new Exception($"Type {fullName} wasn't found in the main AppDomain at this moment.");
119+
}
120+
}
121+
}

Distance.CustomCar/ConfigurationLogic.cs

Lines changed: 0 additions & 52 deletions
This file was deleted.

Distance.CustomCar/Data/Car/CarBuilder.cs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using Distance.CustomCar.Data.Materials;
2-
using Reactor.API;
3-
using Reactor.API.Storage;
42
using System;
53
using System.Collections.Generic;
64
using System.IO;
@@ -24,7 +22,7 @@ public void CreateCars(CarInfos infos)
2422
{
2523
try
2624
{
27-
Mod.Instance.Logger.Info($"Creating car prefab for {car.Key} ...");
25+
Mod.Log.LogInfo($"Creating car prefab for {car.Key} ...");
2826
CreateCarReturnInfos data = CreateCar(car.Value);
2927

3028
string fileName = Path.GetFileNameWithoutExtension(car.Key.Substring(0, car.Key.LastIndexOf('(') - 1));
@@ -34,6 +32,7 @@ public void CreateCars(CarInfos infos)
3432
}
3533
catch (Exception ex)
3634
{
35+
Mod.Log.LogInfo($"Could not load car prefab: {car.Key}");
3736
Mod.Instance.Errors.Add($"Could not load car prefab: {car.Key}");
3837
Mod.Instance.Errors.Add(ex);
3938
}
@@ -44,7 +43,7 @@ public void CreateCars(CarInfos infos)
4443

4544
private void RegisterCars(List<CreateCarReturnInfos> carsInfos)
4645
{
47-
Mod.Instance.Logger.Info($"Registering {carsInfos.Count} car(s)...");
46+
Mod.Log.LogInfo($"Registering {carsInfos.Count} car(s)...");
4847

4948
ProfileManager profileManager = G.Sys.ProfileManager_;
5049
CarInfo[] oldCars = profileManager.carInfos_.ToArray();
@@ -81,10 +80,10 @@ private void RegisterCars(List<CreateCarReturnInfos> carsInfos)
8180
else
8281
{
8382
Mod.Instance.Errors.Add($"A car with the name {car.name_} is already registered, rename the car file if they're the same.");
84-
Mod.Instance.Logger.Warning($"Generating unique name for car {car.name_}");
83+
Mod.Log.LogInfo($"Generating unique name for car {car.name_}");
8584

8685
string uniqueID = $"#{Guid.NewGuid():B}";
87-
Mod.Instance.Logger.Info($"Using GUID: {uniqueID}");
86+
Mod.Log.LogInfo($"Using GUID: {uniqueID}");
8887

8988
car.name_ = $"[FFFF00]![-] {car.name_} {uniqueID}";
9089

@@ -118,7 +117,7 @@ private void RegisterCars(List<CreateCarReturnInfos> carsInfos)
118117
private Dictionary<string, GameObject> LoadAssetsBundles()
119118
{
120119
Dictionary<string, GameObject> assetsList = new Dictionary<string, GameObject>();
121-
DirectoryInfo assetsDirectory = GetLocalFolder(Defaults.PrivateAssetsDirectory);
120+
DirectoryInfo assetsDirectory = GetLocalFolder("Assets");
122121
DirectoryInfo globalCarsDirectory = new DirectoryInfo(Path.Combine(Resource.personalDistanceDirPath_, "CustomCars"));
123122

124123
if (!globalCarsDirectory.Exists)
@@ -130,7 +129,9 @@ private Dictionary<string, GameObject> LoadAssetsBundles()
130129
catch (Exception ex)
131130
{
132131
Mod.Instance.Errors.Add($"Could not create the following folder: {globalCarsDirectory.FullName}");
132+
Mod.Log.LogInfo($"Could not create the following folder: {globalCarsDirectory.FullName}");
133133
Mod.Instance.Errors.Add(ex);
134+
Mod.Log.LogInfo(ex);
134135
}
135136
}
136137

@@ -140,6 +141,7 @@ private Dictionary<string, GameObject> LoadAssetsBundles()
140141
{
141142
Assets assets = Assets.FromUnsafePath(assetsFile.FullName);
142143
AssetBundle bundle = assets.Bundle as AssetBundle;
144+
143145

144146
int foundPrefabCount = 0;
145147

@@ -159,12 +161,15 @@ private Dictionary<string, GameObject> LoadAssetsBundles()
159161
if (foundPrefabCount == 0)
160162
{
161163
Mod.Instance.Errors.Add($"Can't find a prefab in the asset bundle: {assetsFile.FullName}");
164+
Mod.Log.LogInfo($"Can't find a prefab in the asset bundle: {assetsFile.FullName}");
162165
}
163166
}
164167
catch (Exception ex)
165168
{
166169
Mod.Instance.Errors.Add($"Could not load assets file: {assetsFile.FullName}");
170+
Mod.Log.LogInfo($"Could not load assets file: {assetsFile.FullName}");
167171
Mod.Instance.Errors.Add(ex);
172+
Mod.Log.LogInfo(ex);
168173
}
169174
}
170175

@@ -211,12 +216,14 @@ private void RemoveOldCar(GameObject obj)
211216
if (wheelsToRemove.Count != 4)
212217
{
213218
Mod.Instance.Errors.Add($"Found {wheelsToRemove.Count} wheels on base prefabs, expected 4");
219+
Mod.Log.LogInfo($"Found {wheelsToRemove.Count} wheels on base prefabs, expected 4");
214220
}
215221

216222
Transform refractor = obj.transform.Find("Refractor");
217223
if (refractor == null)
218224
{
219225
Mod.Instance.Errors.Add("Can't find the Refractor object on the base car prefab");
226+
Mod.Log.LogInfo("Can't find the Refractor object on the base car prefab");
220227
return;
221228
}
222229

@@ -243,6 +250,7 @@ private void SetColorChanger(ColorChanger colorChanger, GameObject car)
243250
if (colorChanger == null)
244251
{
245252
Mod.Instance.Errors.Add("Can't find the ColorChanger component on the base car");
253+
Mod.Log.LogInfo("Can't find the ColorChanger component on the base car");
246254
return;
247255
}
248256

@@ -280,6 +288,7 @@ private void ReplaceMaterials(Renderer renderer)
280288
if (!infos_.materials.TryGetValue(materialNames[materialIndex], out MaterialInfos materialInfo))
281289
{
282290
Mod.Instance.Errors.Add($"Can't find the material {materialNames[materialIndex]} on {renderer.gameObject.FullName()}");
291+
Mod.Log.LogInfo($"Can't find the material {materialNames[materialIndex]} on {renderer.gameObject.FullName()}");
283292
continue;
284293
}
285294

@@ -339,12 +348,14 @@ private void FillMaterialInfos(Renderer renderer, string[] matNames, List<Materi
339348
if (arguments.Length != 3)
340349
{
341350
Mod.Instance.Errors.Add($"{arguments[0]} property on {renderer.gameObject.FullName()} must have 2 arguments");
351+
Mod.Log.LogInfo($"{arguments[0]} property on {renderer.gameObject.FullName()} must have 2 arguments");
342352
continue;
343353
}
344354

345355
if (!int.TryParse(arguments[1], out int index))
346356
{
347357
Mod.Instance.Errors.Add($"First argument of {arguments[0]} on {renderer.gameObject.FullName()} property must be a number");
358+
Mod.Log.LogInfo($"First argument of {arguments[0]} on {renderer.gameObject.FullName()} property must be a number");
348359
continue;
349360
}
350361

@@ -358,12 +369,14 @@ private void FillMaterialInfos(Renderer renderer, string[] matNames, List<Materi
358369
if (arguments.Length != 5)
359370
{
360371
Mod.Instance.Errors.Add($"{arguments[0]} property on {renderer.gameObject.FullName()} must have 4 arguments");
372+
Mod.Log.LogInfo($"{arguments[0]} property on {renderer.gameObject.FullName()} must have 4 arguments");
361373
continue;
362374
}
363375

364376
if (!int.TryParse(arguments[1], out int index))
365377
{
366378
Mod.Instance.Errors.Add($"First argument of {arguments[0]} on {renderer.gameObject.FullName()} property must be a number");
379+
Mod.Log.LogInfo($"First argument of {arguments[0]} on {renderer.gameObject.FullName()} property must be a number");
367380
continue;
368381
}
369382

@@ -388,6 +401,7 @@ private void FillMaterialInfos(Renderer renderer, string[] matNames, List<Materi
388401
if (!found)
389402
{
390403
Mod.Instance.Errors.Add($"The property {arguments[2]} on {renderer.gameObject.FullName()} is not valid");
404+
Mod.Log.LogInfo($"The property {arguments[2]} on {renderer.gameObject.FullName()} is not valid");
391405
continue;
392406
}
393407

@@ -490,6 +504,7 @@ private void AddMaterialColorChanger(ColorChanger colorChanger, Transform transf
490504
if (arguments.Length != 6)
491505
{
492506
Mod.Instance.Errors.Add($"{arguments[0]} property on {transform.gameObject.FullName()} must have 5 arguments");
507+
Mod.Log.LogInfo($"{arguments[0]} property on {transform.gameObject.FullName()} must have 5 arguments");
493508
continue;
494509
}
495510

@@ -565,6 +580,7 @@ private void SetCarVisuals(CarVisuals visuals, GameObject car)
565580
if (visuals == null)
566581
{
567582
Mod.Instance.Errors.Add("Can't find the CarVisuals component on the base car");
583+
Mod.Log.LogInfo("Can't find the CarVisuals component on the base car");
568584
return;
569585
}
570586

@@ -591,12 +607,14 @@ private void MakeMeshSkinned(SkinnedMeshRenderer renderer)
591607
if (mesh == null)
592608
{
593609
Mod.Instance.Errors.Add($"The mesh on {renderer.gameObject.FullName()} is null");
610+
Mod.Log.LogInfo($"The mesh on {renderer.gameObject.FullName()} is null");
594611
return;
595612
}
596613

597614
if (!mesh.isReadable)
598615
{
599616
Mod.Instance.Errors.Add($"Can't read the car mesh {mesh.name} on {renderer.gameObject.FullName()}You must allow reading on it's unity inspector !");
617+
Mod.Log.LogInfo($"Can't read the car mesh {mesh.name} on {renderer.gameObject.FullName()}You must allow reading on it's unity inspector !");
600618
return;
601619
}
602620

0 commit comments

Comments
 (0)