public static void Load() { ChestSavePath = Path.Combine(ChestControlDirectory, Main.worldID + ".txt"); if (!Directory.Exists(ChestControlDirectory)) Directory.CreateDirectory(ChestControlDirectory); if (!File.Exists(ChestSavePath)) File.Create(ChestSavePath).Close(); for (int i = 0; i < Chests.Length; i++) Chests[i] = new Chest(); bool error = false; foreach ( var args in File.ReadAllLines(ChestSavePath).Select(line => line.Split('|')).Where(args => args.Length >= 7)) try { var chest = new Chest(); chest.SetPosition(new Vector2(int.Parse(args[1]), int.Parse(args[2]))); chest.SetOwner(args[3]); chest.SetID(int.Parse(args[0])); if (bool.Parse(args[4])) chest.Lock(); if (bool.Parse(args[5])) chest.regionLock(true); if (args[6] != "") chest.SetPassword(args[6], true); //provide backwards compatibility if (args.Length == 9) if (bool.Parse(args[7])) { chest.SetRefill(true); //chest.SetRefillItems(args[8]); } //check if chest still exists in world if (!Chest.TileIsChest(chest.GetPosition())) //chest dont exists - so reset it chest.Reset(); //check if chest in array didn't move if (!VerifyChest(chest.GetID(), chest.GetPosition())) { int id = Terraria.Chest.FindChest((int) chest.GetPosition().X, (int) chest.GetPosition().Y); if (id != -1) chest.SetID(id); else chest.Reset(); } if (Chests.Length > chest.GetID()) Chests[chest.GetID()] = chest; } catch { error = true; } if (error) Log.Write("Failed to load some chests data, corresponding chests will be left unprotected.", LogLevel.Error); }
// LoadFromTextFile ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ /* If the legacy file exists, * . read legacy file * . populate the chest info * . save the chest info into the database * . rename the legacy file * . return true * else * . return false - chests will be loaded from db */ private bool LoadFromTextFile() { bool result = false; string ChestControlDirectory = Path.Combine(TShock.SavePath, "chestcontrol"); string ChestSaveFileName = Path.Combine(ChestControlDirectory, Main.worldID + ".txt"); try { if ( Directory.Exists( ChestControlDirectory ) ) { if ( File.Exists( ChestSaveFileName ) ) { Log.Write( "Legacy Data File Found", LogLevel.Info ); bool error = false; foreach ( var args in File.ReadAllLines( ChestSaveFileName ).Select( line => line.Split( '|' ) ).Where( args => args.Length >= 7 ) ) try { var chest = new Chest(); // ID chest.SetID( int.Parse( args[0] ) ); // Position chest.SetPosition( new Vector2( int.Parse( args[1] ), int.Parse( args[2] ) ) ); // Owner chest.SetOwner( args[3] ); // locked if ( bool.Parse( args[4] ) ) chest.Lock(); // region lock if ( bool.Parse( args[5] ) ) chest.regionLock( true ); // password if ( args[6] != "" ) chest.SetPassword( args[6], true ); // provide backwards compatibility if ( args.Length == 9 ) // if refill if ( bool.Parse( args[7] ) ) // refill { chest.SetRefill( true ); //- chest.SetRefillItems(args[8]); // not used - Terraria stores chest contents } // if // check if chest still exists in world if ( !Chest.TileIsChest( chest.GetPosition() ) ) chest.Reset(); // chest doesnt exists - so reset it // check if chest in array didn't move if ( !VerifyChest( chest.GetID(), chest.GetPosition() ) ) { int id = Terraria.Chest.FindChest( (int) chest.GetPosition().X, (int) chest.GetPosition().Y ); if ( id != -1 ) chest.SetID( id ); else chest.Reset(); } // if if ( Chests.Length > chest.GetID() ) { Chests[chest.GetID()] = chest; Log.Write( "[LegacyChest]: " + chest.GetOwner() + "(id:" + chest.GetID() + ")", LogLevel.Info ); } // if } // try catch { error = true; } // catch if ( error ) Log.Write( "Failed to load some chests data, corresponding chests will be left unprotected.", LogLevel.Error ); /* Save the recently loaded chests into database * This will be re-done when the world exits, but if there is a crash, the rename will prevent * loading this information this way again. */ SaveChests(); File.Move( ChestSaveFileName, ChestSaveFileName + "-old.txt" ); result = true; } // if - File.Exists } // if - dir exist } // try catch { Log.Write( "! Error: LoadFromTextFile", LogLevel.Error ); } // catch return result; }
// LoadChests ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ public void LoadChests() { // Initialize local array of all chests in world for our tacking purposes for ( int i = 0; i < Chests.Length; i++ ) Chests[i] = new Chest(); // older versions used text file, if it exists, read that. if ( !LoadFromTextFile() ) { try { using ( var reader = database.QueryReader( "SELECT * FROM " + tableName + " WHERE WorldID=@0", Main.worldID.ToString() ) ) { while ( reader.Read() ) { int chestID = reader.Get<int>( "ChestID" ); int x = reader.Get<int>( "X" ); int y = reader.Get<int>( "Y" ); string owner = reader.Get<string>( "Owner" ); int isLocked = reader.Get<int>( "IsLocked" ); int isRegionLocked = reader.Get<int>( "IsRegionLocked" ); string password = reader.Get<string>( "Password" ); int isRefill = reader.Get<int>( "IsRefill" ); int refillDelay = reader.Get<int>( "RefillDelay" ); Chest chest = new Chest(); chest.SetID( chestID ); chest.SetPosition( new Vector2( x, y ) ); chest.SetOwner( owner ); if ( isLocked != 0 ) { chest.Lock(); } if ( isRegionLocked != 0 ) { chest.regionLock( true ); } if ( password != "" ) { chest.SetPassword( password, true ); } if ( isRefill != 0 ) { chest.SetRefill( true ); } chest.SetRefillDelay( refillDelay ); // check if chest still exists in world if ( !Chest.TileIsChest( chest.GetPosition() ) ) { chest.Reset(); // chest doesnt exist - so reset it } // if // check if chest in array didn't move if ( !VerifyChest( chest.GetID(), chest.GetPosition() ) ) { int id = Terraria.Chest.FindChest( (int) chest.GetPosition().X, (int) chest.GetPosition().Y ); if ( id != -1 ) { Log.Write( "found chest: " + chest.GetID() + ":" + refillDelay, LogLevel.Debug ); chest.SetID( id ); // chest id and contents may have changed if ( isRefill != 0 ) { chest.SetRefill( true ); } // delayed refill stores contents chest.SetRefillDelay( refillDelay ); } else // moved, reset it chest.Reset(); } // if if ( Chests.Length > chest.GetID() ) { Chests[chest.GetID()] = chest; Item[] refillItems = chest.GetRefillItems(); string itemName = "."; itemName = chest.GetFirstItemName(); Log.Write( "[LoadChests]: " + "(id:" + chest.GetID() + ")" + "(rf:" + chest.IsRefill() + ")" + "(rd:" + chest.GetRefillDelay() + ")" + "(1st:" + itemName + ")", LogLevel.Info ); } // if } // while } // using } // try catch ( Exception ex ) { Log.Write( "! [LoadChests]: " + ex.ToString(), LogLevel.Error ); } // catch } // if }
public static void Load() { ChestSavePath = Path.Combine(ChestControlDirectory, Main.worldID + ".txt"); if (!Directory.Exists(ChestControlDirectory)) { Directory.CreateDirectory(ChestControlDirectory); } if (!File.Exists(ChestSavePath)) { File.Create(ChestSavePath).Close(); } for (int i = 0; i < Chests.Length; i++) { Chests[i] = new Chest(); } bool error = false; foreach ( var args in File.ReadAllLines(ChestSavePath).Select(line => line.Split('|')).Where(args => args.Length >= 7)) { try { var chest = new Chest(); chest.SetPosition(new Vector2(int.Parse(args[1]), int.Parse(args[2]))); chest.SetOwner(args[3]); chest.SetID(int.Parse(args[0])); if (bool.Parse(args[4])) { chest.Lock(); } if (bool.Parse(args[5])) { chest.regionLock(true); } if (args[6] != "") { chest.SetPassword(args[6], true); } //provide backwards compatibility if (args.Length == 9) { if (bool.Parse(args[7])) { chest.SetRefill(true); //chest.SetRefillItems(args[8]); } } //check if chest still exists in world if (!Chest.TileIsChest(chest.GetPosition())) { //chest dont exists - so reset it chest.Reset(); } //check if chest in array didn't move if (!VerifyChest(chest.GetID(), chest.GetPosition())) { int id = Terraria.Chest.FindChest((int)chest.GetPosition().X, (int)chest.GetPosition().Y); if (id != -1) { chest.SetID(id); } else { chest.Reset(); } } if (Chests.Length > chest.GetID()) { Chests[chest.GetID()] = chest; } } catch { error = true; } } if (error) { Log.Write("Failed to load some chests data, corresponding chests will be left unprotected.", LogLevel.Error); } }