using System; using System.Collections.Generic; using System.IO; using Obvious.Soap; using UFS3; using UnityEngine; public class PlayerProfile : ScriptableObject { [Serializable] private struct ProfileSerialize { public int ID; public string Name; public int Level; public int XP; public int TotalXP; public float Money; } [Serializable] private struct InventorySerialized { public int ID; public float Durability; public InventorySerialized(int id, float durability) { ID = id; Durability = durability; } } [Serializable] private struct FishingSetSerialize { public InventorySerialized Rod; public InventorySerialized Reel; public InventorySerialized Line; public InventorySerialized Bobber; public InventorySerialized Lure; } [Serializable] public struct FishSerialized { public int ID; public float Mass; public FishSerialized(int id, float mass) { ID = id; Mass = mass; } } public bool IsDebugModeOn; public bool AutoSave; public string Name = "Angler"; public int Level = 1; public int XP; public int TotalXP = 50; public FloatVariable MoneyVar; public ScriptableEventNoParam OnPlayerProfileNameExist; public ScriptableEventNoParam OnFishRemovedFromNet; public ScriptableListItemData Inventory; public ScriptableListFishingSetData FishingSets; public ItemDatabase ItemDatabase; public FishDatabase FishDatabase; public BoolVariable IsFishRecordRegistred; public StringVariable LastLoadedChapterID; [SerializeField] private List _FishInNet; [SerializeField] private List _FishRecords; public float Money { get { return MoneyVar.Value; } set { MoneyVar.Value = value; SaveData(); } } public List FishRecords { get { List list = new List(); foreach (FishSerialized fishRecord in _FishRecords) { FishData newInstanceByID = FishDatabase.GetNewInstanceByID(fishRecord.ID); newInstanceByID.Weight = fishRecord.Mass; list.Add(newInstanceByID); } return list; } } public List FishInNet { get { List list = new List(); foreach (FishSerialized item in _FishInNet) { FishData newInstanceByID = FishDatabase.GetNewInstanceByID(item.ID); newInstanceByID.Weight = item.Mass; list.Add(newInstanceByID); } return list; } } public event Action OnLoadData; public event Action OnProfileCreated; private void AddFishDebug(FishData fish) { AddFish(fish); } public void SaveData() { ES3Settings.defaultSettings.path = Name + ".sav"; PlayerPrefs.SetString("PlayerLastProfileLoaded", Name); ES3.Save("Money", Money); InventorySave(); FishingSetsSave(); FishDatabase.ValidateIDs(); ES3.Save("Fish", _FishInNet); ES3.Save("FishRecords", _FishRecords); ES3.Save("LastLoadedChapterID", LastLoadedChapterID.Value); void FishingSetsSave() { new List(); int num = 0; foreach (FishingSetData fishingSet in FishingSets) { ES3.Save($"FishingSet_{num}_MethodType", fishingSet.SetType); SetItemSave(fishingSet.Rod, $"{num}_Rod"); SetItemSave(fishingSet.Reel, $"{num}_Reel"); SetItemSave(fishingSet.Line, $"{num}_Line"); SetItemSave(fishingSet.Bobber, $"{num}_Bobber"); SetItemSave(fishingSet.Lure, $"{num}_Lure"); SetItemSave(fishingSet.Hook, $"{num}_Hook"); SetItemSave(fishingSet.Bait, $"{num}_Bait"); SetItemSave(fishingSet.Weight, $"{num}_Weight"); num++; } } void InventorySave() { List list = new List(); ItemDatabase.ValidateIDs(); foreach (BaseItemData item in Inventory) { int iD = item.ID; float durabilty = item.Durabilty; list.Add(new InventorySerialized(iD, durabilty)); } ES3.Save("Inventory", list); } static void SetItemSave(BaseItemData item, string key) { if (item != null) { ES3.Save("FishingSet_" + key, new InventorySerialized(item.ID, item.Durabilty)); } else { ES3.Save("FishingSet_" + key, new InventorySerialized(-1, 0f)); } } } public float GetMoneyFromProfile(string name) { return ES3.Load("Money", name + ".sav"); } public bool LoadData() { ES3Settings.defaultSettings.path = Name + ".sav"; if (!ES3.FileExists(Name + ".sav")) { OnPlayerProfileNameExist.Raise(); Debug.Log("Profile with same name not exists! Create new Profile!"); return false; } PlayerPrefs.SetString("PlayerLastProfileLoaded", Name); LoadInventoryData(); LoadFishingSets(); _FishInNet.Clear(); _FishInNet = ES3.Load("Fish", new List()); _FishRecords.Clear(); _FishRecords = ES3.Load("FishRecords", new List()); MoneyVar.Value = ES3.Load("Money"); if (ES3.KeyExists("GameVersion")) { string text = ES3.Load("GameVersion"); Debug.Log("Loaded Game, save in version: " + text); } else { Debug.Log("No Game Version found!, it's look an old save file!"); } LastLoadedChapterID.Value = ES3.Load("LastLoadedChapterID", ""); this.OnLoadData?.Invoke(); return true; void LoadFishingSets() { FishingSets.Clear(); InventorySerialized defaultValue = new InventorySerialized(-1, 0f); for (int i = 0; i < 2; i++) { FishingSets.Add(ScriptableObject.CreateInstance()); FishingSets[i].SetType = ES3.Load($"FishingSet_{i}_MethodType", FishingMethodType.NotComplete); FishingSets[i].Rod = (RodData)LoadItem(ES3.Load($"FishingSet_{i}_Rod", defaultValue)); FishingSets[i].Reel = (ReelData)LoadItem(ES3.Load($"FishingSet_{i}_Reel", defaultValue)); FishingSets[i].Line = (LineData)LoadItem(ES3.Load($"FishingSet_{i}_Line", defaultValue)); FishingSets[i].Bobber = (BobberData)LoadItem(ES3.Load($"FishingSet_{i}_Bobber", defaultValue)); FishingSets[i].Lure = (LureData)LoadItem(ES3.Load($"FishingSet_{i}_Lure", defaultValue)); FishingSets[i].Bait = (BaitData)LoadItem(ES3.Load($"FishingSet_{i}_Bait", defaultValue)); FishingSets[i].Hook = (HookData)LoadItem(ES3.Load($"FishingSet_{i}_Hook", defaultValue)); FishingSets[i].Weight = (WeightData)LoadItem(ES3.Load($"FishingSet_{i}_Weight", defaultValue)); } } void LoadInventoryData() { List list = ES3.Load("Inventory", new List()); Inventory.Clear(); foreach (InventorySerialized item in list) { int iD = item.ID; BaseItemData itemById = ItemDatabase.GetItemById(iD); if (itemById != null) { BaseItemData baseItemData = UnityEngine.Object.Instantiate(itemById); baseItemData.ID = iD; baseItemData.Durabilty = item.Durability; Inventory.Add(baseItemData); } else { Debug.LogError($"Item with ID {iD} not found!, check database where empty row, or ignore"); } } } BaseItemData LoadItem(InventorySerialized invItem) { if (invItem.ID >= 0) { BaseItemData baseItemData = UnityEngine.Object.Instantiate(ItemDatabase.GetItemById(invItem.ID)); baseItemData.ID = invItem.ID; baseItemData.Durabilty = invItem.Durability; return baseItemData; } return null; } } public void SellFish(int id) { FishSerialized fishSerialized = _FishInNet[id]; Money += fishSerialized.Mass; Debug.Log($"Fish sell for: {fishSerialized.Mass}$"); _FishInNet.RemoveAt(id); OnFishRemovedFromNet.Raise(); SaveData(); } public void SellItemFromInventory(BaseItemData itemToRemove) { int index = Inventory.IndexOf(itemToRemove); Money += itemToRemove.Price; Debug.Log($"Item sell for: {itemToRemove.Price}$"); Inventory.RemoveAt(index); SaveData(); } public string[] GetProfiles() { string[] files = ES3.GetFiles(); List list = new List(); string[] array = files; foreach (string path in array) { if (!(Path.GetExtension(path) != ".sav")) { string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(path); list.Add(fileNameWithoutExtension); } } return list.ToArray(); } public bool TryCreateProfile(string name) { if (ES3.FileExists(name + ".sav")) { OnPlayerProfileNameExist.Raise(); Debug.Log("Profile with same name already exists!"); return false; } Name = name; Inventory.Clear(); FishingSets.Clear(); _FishInNet.Clear(); _FishRecords.Clear(); MoneyVar.Value = 1000000f; for (int i = 0; i < 2; i++) { FishingSets.Add(ScriptableObject.CreateInstance()); } LastLoadedChapterID.Value = ""; SaveData(); ES3.Save("GameVersion", Application.version); this.OnProfileCreated?.Invoke(); return true; } public bool CheckIsExistProfile(string Name) { if (!ES3.FileExists(Name + ".sav")) { Debug.Log("Profile with same name not exists! Create new Profile!"); return false; } return true; } public bool LoadData(string name) { Name = name; return LoadData(); } public void DeleteProfile(string name) { string filePath = name + ".sav"; if (!ES3.FileExists(filePath)) { Debug.LogError("Profile does not exist!"); } ES3.DeleteFile(filePath); } public void RemoveFishingSet(FishingSetData fishingSet) { if ((bool)fishingSet.Rod) { Inventory.Add(fishingSet.Rod); } if ((bool)fishingSet.Reel) { Inventory.Add(fishingSet.Reel); } if ((bool)fishingSet.Line) { Inventory.Add(fishingSet.Line); } if ((bool)fishingSet.Bobber) { Inventory.Add(fishingSet.Bobber); } if ((bool)fishingSet.Hook) { Inventory.Add(fishingSet.Hook); } if ((bool)fishingSet.Weight) { Inventory.Add(fishingSet.Weight); } if ((bool)fishingSet.Lure) { Inventory.Add(fishingSet.Lure); } if ((bool)fishingSet.Bait) { Inventory.Add(fishingSet.Bait); } fishingSet.Rod = null; fishingSet.Reel = null; fishingSet.Line = null; fishingSet.Bobber = null; fishingSet.Hook = null; fishingSet.Weight = null; fishingSet.Lure = null; fishingSet.Bait = null; SaveData(); } public void AddItem(BaseItemData itemData, bool overrideAutoSave = false) { Inventory.Add(itemData); if (AutoSave && !overrideAutoSave) { SaveData(); } } public void AddFish(FishData fishData, bool overrideAutoSave = false) { if (fishData.ID < 0) { Debug.LogError("Fish ID is negative check if fish in database "); return; } _FishInNet.Add(new FishSerialized(fishData.ID, fishData.Weight)); if (AutoSave && !overrideAutoSave) { SaveData(); } } public void CheckFishRecord(FishData fishData) { bool flag = false; foreach (FishSerialized fishRecord in _FishRecords) { if (fishRecord.ID == fishData.ID) { if (flag) { Debug.LogError("Fish record already exists in save check why!"); } flag = true; } } if (flag) { for (int i = 0; i < _FishRecords.Count; i++) { FishSerialized value = _FishRecords[i]; if (value.ID == fishData.ID && fishData.Weight > value.Mass) { value.Mass = fishData.Weight; _FishRecords[i] = value; IsFishRecordRegistred.Value = true; SaveData(); break; } } } else { _FishRecords.Add(new FishSerialized(fishData.ID, fishData.Weight)); IsFishRecordRegistred.Value = true; SaveData(); } } public void DestroyItem(BaseItemData itemData) { } }