예제 #1
0
 public static byte[] ToArray(this IKn5 kn5, bool includeTextures = true)
 {
     using (var memory = new MemoryStream()) {
         if (!includeTextures)
         {
             kn5.Textures.Clear();
             kn5.TexturesData.Clear();
         }
         kn5.Save(memory);
         return(memory.ToArray());
     }
 }
예제 #2
0
        public static async Task UpdateKn5(this IKn5 kn5, BaseRenderer renderer = null, CarSkinObject skin = null)
        {
            if (!kn5.IsEditable)
            {
                throw new Exception("Can’t save KN5 loaded unusually");
            }

            var backup = kn5.OriginalFilename + ".backup";

            try {
                if (!File.Exists(backup))
                {
                    FileUtils.HardLinkOrCopy(kn5.OriginalFilename, backup);
                }
            } catch (Exception e) {
                Logging.Warning(e);
            }

            await Task.Run(() => {
                using (var f = FileUtils.RecycleOriginal(kn5.OriginalFilename)) {
                    try {
                        kn5.Save(f.Filename);
                    } catch {
                        FileUtils.TryToDelete(f.Filename);
                        throw;
                    }
                }
            });

            if (renderer != null)
            {
                var car  = skin == null ? null : CarsManager.Instance.GetById(skin.CarId);
                var slot = (renderer as ToolsKn5ObjectRenderer)?.MainSlot;
                if (car != null && slot != null)
                {
                    slot.SetCar(CarDescription.FromKn5(kn5, car.Location, car.AcdData));
                    slot.SelectSkin(skin.Id);
                }
            }
        }