| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- using System;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Security.Cryptography;
- using UnityEngine;
- namespace BuffaloSiege
- {
- [Serializable] public class PlayerSave
- {
- public int version=1,coins=1500,cleared,bestScore,bestWave,kills,towers,waves,checkinDay=1,tutorial;
- public int[] items={1,0,0,1},armory={1,1,1,1,1},stars=new int[20];
- public string playerName="Desert Ranger",playerId=NewId(),avatar="bison",lastCheckin="";
- static string NewId()=>((BitConverter.ToUInt32(Guid.NewGuid().ToByteArray(),0)%90000000)+10000000).ToString(System.Globalization.CultureInfo.InvariantCulture);
- public bool sfx=true,music;
- public void Validate()
- {
- coins=Mathf.Clamp(coins,0,2000000000);cleared=Mathf.Clamp(cleared,0,20);checkinDay=Mathf.Clamp(checkinDay,1,7);
- items=Fix(items,4,0,0,99999);armory=Fix(armory,5,1,1,5);stars=Fix(stars,20,0,0,3);
- for(int i=0;i<20;i++)if(stars[i]>0)cleared=Math.Max(cleared,i+1);
- bestScore=Math.Max(0,bestScore);bestWave=Math.Max(0,bestWave);kills=Math.Max(0,kills);towers=Math.Max(0,towers);waves=Math.Max(0,waves);
- if(string.IsNullOrEmpty(playerName)||playerName.Length>20)playerName="Desert Ranger";
- if(string.IsNullOrEmpty(playerId))playerId=NewId();
- if(!GameData.Avatars.Contains(avatar))avatar="bison";
- }
- static int[] Fix(int[] src,int count,int fallback,int min,int max){var v=new int[count];for(int i=0;i<count;i++)v[i]=Mathf.Clamp(src!=null&&i<src.Length?src[i]:fallback,min,max);return v;}
- }
- [Serializable] class SaveEnvelope {public string payload,checksum;}
- public sealed class SaveStore
- {
- public PlayerSave Data {get;private set;} public string LastError{get;private set;} public bool Recovered{get;private set;} readonly string path;
- public SaveStore(string filePath){path=filePath;Data=Read(path)??Read(path==null?null:path+".bak")??new PlayerSave();Data.Validate();}
- PlayerSave Read(string p)
- {
- if(p==null||!File.Exists(p))return null;
- try{var e=JsonUtility.FromJson<SaveEnvelope>(File.ReadAllText(p));if(e==null||e.checksum!=Hash(e.payload))throw new InvalidDataException("Checksum mismatch");var d=JsonUtility.FromJson<PlayerSave>(e.payload);if(d==null||d.version>1)throw new InvalidDataException("Unsupported save version");Recovered=p.EndsWith(".bak");return d;}catch(Exception ex){LastError=ex.Message;return null;}
- }
- static string Hash(string s){using(var hash=SHA256.Create())return Convert.ToBase64String(hash.ComputeHash(Encoding.UTF8.GetBytes(s??"")));}
- public bool Save()
- {
- if(path==null)return true;
- try
- {
- Directory.CreateDirectory(Path.GetDirectoryName(path));string payload=JsonUtility.ToJson(Data);string text=JsonUtility.ToJson(new SaveEnvelope{payload=payload,checksum=Hash(payload)});
- File.WriteAllText(path+".tmp",text,new UTF8Encoding(false));
- if(File.Exists(path)){try{File.Replace(path+".tmp",path,path+".bak");}catch(PlatformNotSupportedException){File.Copy(path,path+".bak",true);File.Copy(path+".tmp",path,true);File.Delete(path+".tmp");}}
- else File.Move(path+".tmp",path);LastError=null;return true;
- }catch(Exception ex){LastError=ex.Message;Debug.LogWarning("Buffalo Siege could not save: "+ex.Message);return false;}
- }
- public void Reset(){Data=new PlayerSave();Save();}
- public bool Buy(int item){if(item<0||item>=4||Data.coins<GameData.Items[item].Price)return false;Data.coins-=GameData.Items[item].Price;Data.items[item]++;Save();return true;}
- public bool Consume(int item){if(item<0||item>=4||Data.items[item]<=0)return false;Data.items[item]--;Save();return true;}
- public bool Upgrade(int type){int lv=Data.armory[type],cost=GameData.ArmoryCost(type,lv);if(lv>=5||Data.coins<cost)return false;Data.coins-=cost;Data.armory[type]++;Save();return true;}
- public bool CanClaim(DateTime now)=>String.CompareOrdinal(Data.lastCheckin,now.ToString("yyyy-MM-dd"))<0;
- public bool Claim(DateTime now)
- {if(!CanClaim(now))return false;int d=Data.checkinDay-1;int[] rewards={300,0,600,1000,0,1500,2500};Data.coins+=rewards[d];if(d==1)Data.items[0]++;if(d==4)Data.items[2]++;Data.checkinDay=d==6?1:d+2;Data.lastCheckin=now.ToString("yyyy-MM-dd");Save();return true;}
- public LeaderEntry[] Board(){return GameData.Bots.Concat(new[]{new LeaderEntry(Data.playerName,Data.bestScore,Data.bestWave,true)}).OrderByDescending(x=>x.score).ThenByDescending(x=>x.wave).ToArray();}
- }
- }
|