Skip to content
This repository has been archived by the owner on Jan 6, 2022. It is now read-only.

Commit

Permalink
Fixed a bug with loading cars with the same name
Browse files Browse the repository at this point in the history
  • Loading branch information
REHERC committed Dec 24, 2020
1 parent 62165ed commit a4bcc86
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 13 deletions.
58 changes: 46 additions & 12 deletions Distance.CustomCar/Data/Car/CarBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public void CreateCars(CarInfos infos)
{
Mod.Instance.Logger.Info($"Creating car prefab for {car.Key} ...");
CreateCarReturnInfos data = CreateCar(car.Value);

string fileName = Path.GetFileNameWithoutExtension(car.Key.Substring(0, car.Key.LastIndexOf('(') - 1));
data.car.name = fileName;

carsInfos.Add(data);
}
catch (Exception ex)
Expand All @@ -35,9 +39,16 @@ public void CreateCars(CarInfos infos)
}
}

RegisterCars(carsInfos);
}

private void RegisterCars(List<CreateCarReturnInfos> carsInfos)
{
Mod.Instance.Logger.Info($"Registering {carsInfos.Count} car(s)...");

ProfileManager profileManager = G.Sys.ProfileManager_;
CarInfo[] oldCars = profileManager.carInfos_.ToArray();
profileManager.carInfos_ = new CarInfo[oldCars.Length + cars.Count];
profileManager.carInfos_ = new CarInfo[oldCars.Length + carsInfos.Count];

ref Dictionary<string, int> unlocked = ref profileManager.unlockedCars_;
ref Dictionary<string, int> knowCars = ref profileManager.knownCars_;
Expand All @@ -50,34 +61,51 @@ public void CreateCars(CarInfos infos)
continue;
}

int index = carIndex - oldCars.Length;
int infoIndex = carIndex - oldCars.Length;

CarInfo car = new CarInfo
{
name_ = carsInfos[index].car.name,
name_ = carsInfos[infoIndex].car.name,
prefabs_ = new CarPrefabs
{
carPrefab_ = carsInfos[index].car
carPrefab_ = carsInfos[infoIndex].car
},
colors_ = carsInfos[index].colors
colors_ = carsInfos[infoIndex].colors
};

if (!knowCars.ContainsKey(car.name_) && !unlocked.ContainsKey(car.name_))
{
unlocked.Add(car.name_, carIndex);
knowCars.Add(car.name_, carIndex);
}
else
{
Mod.Instance.Errors.Add($"A car with the name {car.name_} is already registered, rename the car file if they're the same.");
Mod.Instance.Logger.Warning($"Generating unique name for car {car.name_}");

string uniqueID = $"#{Guid.NewGuid():B}";
Mod.Instance.Logger.Info($"Using GUID: {uniqueID}");

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

unlocked.Add(car.name_, carIndex);
knowCars.Add(car.name_, carIndex);
}

profileManager.carInfos_[carIndex] = car;
unlocked.Add(car.name_, carIndex);
knowCars.Add(car.name_, carIndex);
}

CarColors[] carColors = new CarColors[oldCars.Length + cars.Count];
CarColors[] carColors = new CarColors[oldCars.Length + carsInfos.Count];
for (int colorIndex = 0; colorIndex < carColors.Length; colorIndex++)
{
carColors[colorIndex] = G.Sys.ProfileManager_.carInfos_[colorIndex].colors_;
}

for (int profileIndex = 0; profileIndex < profileManager.ProfileCount_; profileIndex++)
{
Profile profile = profileManager.GetProfile(profileIndex);

CarColors[] oldColorList = profile.carColorsList_; ;
CarColors[] oldColorList = profile.carColorsList_;

for (int oldColorIndex = 0; oldColorIndex < oldColorList.Length && oldColorIndex < carColors.Length; oldColorIndex++)
{
carColors[oldColorIndex] = oldColorList[oldColorIndex];
Expand All @@ -104,8 +132,14 @@ private Dictionary<string, GameObject> LoadAssetsBundles()
foreach (string assetName in from name in bundle.GetAllAssetNames() where name.EndsWith(".prefab", StringComparison.InvariantCultureIgnoreCase) select name)
{
GameObject carPrefab = bundle.LoadAsset<GameObject>(assetName);
assetsList.Add($"{assetsFile.FullName} ({assetName})", carPrefab);
foundPrefabCount++;

string assetKey = $"{assetsFile.FullName} ({assetName})";

if (!assetsList.ContainsKey(assetKey))
{
assetsList.Add(assetKey, carPrefab);
foundPrefabCount++;
}
}

if (foundPrefabCount == 0)
Expand Down
2 changes: 1 addition & 1 deletion Distance.CustomCar/Data/Errors/ErrorList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void Show()
{
if (this.Any())
{
string message = string.Join(Environment.NewLine, ToArray());
string message = Count < 15 ? string.Join(Environment.NewLine, ToArray()) : "There were too many errors when loading custom cars to be displayed here, please check the logs in your mod installation directory.";

MessageBox.Create($"Can't load the cars correctly: {Count} error(s)\n{message}", "CUSTOM CARS - ERRORS")
.SetButtons(MessageButtons.Ok)
Expand Down

0 comments on commit a4bcc86

Please sign in to comment.