From a4bcc86023b279ee256eb4feb7e66fc665323adc Mon Sep 17 00:00:00 2001 From: Reherc Date: Thu, 24 Dec 2020 02:19:27 +0100 Subject: [PATCH] Fixed a bug with loading cars with the same name --- Distance.CustomCar/Data/Car/CarBuilder.cs | 58 ++++++++++++++++----- Distance.CustomCar/Data/Errors/ErrorList.cs | 2 +- 2 files changed, 47 insertions(+), 13 deletions(-) diff --git a/Distance.CustomCar/Data/Car/CarBuilder.cs b/Distance.CustomCar/Data/Car/CarBuilder.cs index 0adfe6b..1caae62 100644 --- a/Distance.CustomCar/Data/Car/CarBuilder.cs +++ b/Distance.CustomCar/Data/Car/CarBuilder.cs @@ -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) @@ -35,9 +39,16 @@ public void CreateCars(CarInfos infos) } } + RegisterCars(carsInfos); + } + + private void RegisterCars(List 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 unlocked = ref profileManager.unlockedCars_; ref Dictionary knowCars = ref profileManager.knownCars_; @@ -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]; @@ -104,8 +132,14 @@ private Dictionary LoadAssetsBundles() foreach (string assetName in from name in bundle.GetAllAssetNames() where name.EndsWith(".prefab", StringComparison.InvariantCultureIgnoreCase) select name) { GameObject carPrefab = bundle.LoadAsset(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) diff --git a/Distance.CustomCar/Data/Errors/ErrorList.cs b/Distance.CustomCar/Data/Errors/ErrorList.cs index d153c6a..e4b2408 100644 --- a/Distance.CustomCar/Data/Errors/ErrorList.cs +++ b/Distance.CustomCar/Data/Errors/ErrorList.cs @@ -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)