2018-06-13 17:36:52 +08:00
using Microsoft.Xna.Framework ;
using Microsoft.Xna.Framework.Input ;
2017-09-13 02:20:18 +08:00
using StardewModdingAPI ;
using StardewValley ;
using StardewValley.Menus ;
2018-08-08 15:12:31 +08:00
using StardewValley.Network ;
2018-08-23 16:23:42 +08:00
using StardustCore.Menus ;
2017-09-12 14:39:59 +08:00
using StardustCore.ModInfo ;
2018-08-08 15:12:31 +08:00
using StardustCore.NetCode ;
2018-06-13 17:36:52 +08:00
using StardustCore.Objects ;
2018-06-13 09:42:31 +08:00
using StardustCore.Objects.Tools ;
2017-09-14 04:53:12 +08:00
using StardustCore.Serialization ;
2018-06-13 14:25:36 +08:00
using StardustCore.UIUtilities ;
2018-03-30 20:46:24 +08:00
using StardustCore.UIUtilities.SpriteFonts ;
2017-09-05 19:13:42 +08:00
using System ;
2017-07-14 11:27:48 +08:00
using System.Collections.Generic ;
2017-09-14 04:53:12 +08:00
using System.IO ;
2017-07-14 11:27:48 +08:00
using System.Linq ;
2018-08-08 15:12:31 +08:00
using System.Reflection ;
2017-07-14 11:27:48 +08:00
using System.Text ;
using System.Threading.Tasks ;
2017-09-05 19:13:42 +08:00
namespace StardustCore
2017-07-14 11:27:48 +08:00
{
2017-09-05 19:13:42 +08:00
public class ModCore : Mod
2017-07-14 11:27:48 +08:00
{
2017-09-12 09:35:31 +08:00
public static IModHelper ModHelper ;
public static IMonitor ModMonitor ;
2018-08-09 04:18:51 +08:00
public static IManifest Manifest ;
2017-09-14 04:53:12 +08:00
public static Serialization . SerializationManager SerializationManager ;
2018-06-13 17:36:52 +08:00
public static UIUtilities . TextureManager TextureManager ;
2018-08-09 04:18:51 +08:00
public static Dictionary < string , TextureManager > TextureManagers ;
2018-08-23 16:23:42 +08:00
2018-02-06 17:27:28 +08:00
2018-08-08 15:12:31 +08:00
public static Multiplayer multiplayer ;
bool serverHack ;
2018-08-09 00:32:02 +08:00
private Type lastMenuType ;
2018-08-21 04:53:56 +08:00
public ModConfig config ;
2018-02-06 17:27:28 +08:00
public static string ContentDirectory ;
2017-09-12 09:35:31 +08:00
public override void Entry ( IModHelper helper )
{
2018-06-13 09:42:31 +08:00
ModHelper = Helper ;
2018-02-06 17:27:28 +08:00
ModMonitor = Monitor ;
2018-08-09 04:18:51 +08:00
Manifest = ModManifest ;
2017-09-13 07:30:30 +08:00
//Unused MetaData information. Works in player inventory but not in chests. Besides who really care where an object is from anyways. Also doesn't work 100% like I intended since it only gets base mod object that this runs from, not extensions?
2017-09-14 04:53:12 +08:00
// StardewModdingAPI.Events.GraphicsEvents.OnPostRenderGuiEvent += Metadata.GameEvents_UpdateTick;
2018-02-06 17:27:28 +08:00
//StardewModdingAPI.Events.ControlEvents.MouseChanged += ControlEvents_MouseChanged;
2018-05-01 09:21:31 +08:00
string invPath = Path . Combine ( ModCore . ModHelper . DirectoryPath , "PlayerData" , Game1 . player . Name , "PlayerInventory" ) ;
string worldPath = Path . Combine ( ModCore . ModHelper . DirectoryPath , Game1 . player . Name , "ObjectsInWorld" ) ; ;
2017-09-14 04:53:12 +08:00
string trashPath = Path . Combine ( ModCore . ModHelper . DirectoryPath , "ModTrashFolder" ) ;
string chestPath = Path . Combine ( ModCore . ModHelper . DirectoryPath , "StorageContainers" ) ;
SerializationManager = new SerializationManager ( invPath , trashPath , worldPath , chestPath ) ;
StardewModdingAPI . Events . SaveEvents . AfterSave + = SaveEvents_AfterSave ;
StardewModdingAPI . Events . SaveEvents . BeforeSave + = SaveEvents_BeforeSave ;
StardewModdingAPI . Events . SaveEvents . AfterLoad + = SaveEvents_AfterLoad ;
2018-08-09 00:32:02 +08:00
StardewModdingAPI . Events . MenuEvents . MenuChanged + = MenuEvents_MenuChanged ;
StardewModdingAPI . Events . MenuEvents . MenuClosed + = MenuEvents_MenuClosed ;
2017-11-17 15:07:30 +08:00
IlluminateFramework . Colors . initializeColors ( ) ;
2018-08-14 03:41:11 +08:00
ContentDirectory = "Content" ;
if ( ! Directory . Exists ( ContentDirectory ) ) Directory . CreateDirectory ( Path . Combine ( ModHelper . DirectoryPath , "Content" ) ) ;
2018-03-30 20:46:24 +08:00
SpriteFonts . initialize ( ) ;
2018-06-13 09:42:31 +08:00
SerializationManager . initializeDefaultSuportedTypes ( ) ;
2018-08-09 04:18:51 +08:00
TextureManagers = new Dictionary < string , TextureManager > ( ) ;
2018-06-13 17:36:52 +08:00
TextureManager = new TextureManager ( ) ;
2018-08-09 04:18:51 +08:00
TextureManager . addTexture ( "Test1.png" , new Texture2DExtended ( ModCore . ModHelper , Manifest , Path . Combine ( "Content" , "Graphics" , "MultiTest" , "Test1.png" ) ) ) ;
TextureManagers . Add ( ModManifest . UniqueID , TextureManager ) ;
2018-08-08 01:07:33 +08:00
StardewModdingAPI . Events . ControlEvents . KeyPressed + = ControlEvents_KeyPressed ;
2018-08-21 04:53:56 +08:00
config = ModHelper . ReadConfig < ModConfig > ( ) ;
2018-08-08 01:07:33 +08:00
StardewModdingAPI . Events . GameEvents . UpdateTick + = GameEvents_UpdateTick ;
2018-08-08 15:12:31 +08:00
serverHack = false ;
}
2018-08-09 00:32:02 +08:00
private void MenuEvents_MenuClosed ( object sender , StardewModdingAPI . Events . EventArgsClickableMenuClosed e )
{
2018-08-21 04:53:56 +08:00
if ( Game1 . IsMasterGame = = false & & config . enableMultiplayerHack )
2018-08-09 00:32:02 +08:00
{
if ( this . lastMenuType = = null )
{
return ;
}
else
{
if ( lastMenuType = = typeof ( StardewValley . Menus . SaveGameMenu ) | | lastMenuType = = typeof ( StardewValley . Menus . ReadyCheckDialog ) )
{
SerializationManager . restoreAllModObjects ( SerializationManager . trackedObjectList ) ;
}
}
}
}
private void MenuEvents_MenuChanged ( object sender , StardewModdingAPI . Events . EventArgsClickableMenuChanged e )
{
2018-08-21 04:53:56 +08:00
if ( Game1 . IsMasterGame = = false & & config . enableMultiplayerHack )
2018-08-09 00:32:02 +08:00
{
if ( Game1 . activeClickableMenu . GetType ( ) = = typeof ( StardewValley . Menus . ReadyCheckDialog ) )
{
SerializationManager . cleanUpInventory ( ) ;
SerializationManager . cleanUpWorld ( ) ;
SerializationManager . cleanUpStorageContainers ( ) ;
}
}
lastMenuType = Game1 . activeClickableMenu . GetType ( ) ;
}
2018-08-08 15:12:31 +08:00
/// <summary>
/// Returns the value of the data snagged by reflection.
/// </summary>
/// <param name="type"></param>
/// <param name="instance"></param>
/// <param name="fieldName"></param>
/// <returns></returns>
public static object GetInstanceField ( Type type , object instance , string fieldName )
{
BindingFlags bindFlags = BindingFlags . Instance | BindingFlags . Public | BindingFlags . NonPublic | BindingFlags . Static ;
FieldInfo field = type . GetField ( fieldName , bindFlags ) ;
/ *
FieldInfo [ ] meh = type . GetFields ( bindFlags ) ;
foreach ( var v in meh )
{
if ( v . Name = = null )
{
continue ;
}
Monitor . Log ( v . Name ) ;
}
* /
return field . GetValue ( instance ) ;
}
public static void SetInstanceField ( Type type , object instance , object value , string fieldName )
{
BindingFlags bindFlags = BindingFlags . Instance | BindingFlags . Public | BindingFlags . NonPublic
| BindingFlags . Static ;
FieldInfo field = type . GetField ( fieldName , bindFlags ) ;
field . SetValue ( instance , value ) ;
return ;
2017-09-14 04:53:12 +08:00
}
2018-08-08 01:07:33 +08:00
private void GameEvents_UpdateTick ( object sender , EventArgs e )
{
2018-08-08 15:12:31 +08:00
if ( Game1 . activeClickableMenu ! = null )
{
2018-08-21 04:53:56 +08:00
if ( Game1 . activeClickableMenu is StardewValley . Menus . TitleMenu & & config . enableMultiplayerHack )
2018-08-08 15:12:31 +08:00
{
if ( TitleMenu . subMenu = = null ) return ;
if ( TitleMenu . subMenu . GetType ( ) = = typeof ( FarmhandMenu ) )
{
if ( ( TitleMenu . subMenu as FarmhandMenu ) . client . GetType ( ) ! = typeof ( ModdedClient ) )
{
ModCore . ModMonitor . Log ( "OK!" ) ;
multiplayer = ( Multiplayer ) GetInstanceField ( typeof ( Game1 ) , Program . gamePtr , "multiplayer" ) ;
ModCore . ModMonitor . Log ( "CODE!!!!!!!" ) ;
string address = ( string ) GetInstanceField ( typeof ( LidgrenClient ) , ( TitleMenu . subMenu as FarmhandMenu ) . client , "address" ) ;
( TitleMenu . subMenu as FarmhandMenu ) . client = new NetCode . ModdedClient ( address ) ;
}
}
}
}
2018-08-21 04:53:56 +08:00
if ( Game1 . server ! = null & & serverHack = = false & & config . enableMultiplayerHack )
2018-08-08 15:12:31 +08:00
{
ModCore . ModMonitor . Log ( "OK!" ) ;
multiplayer = ( Multiplayer ) GetInstanceField ( typeof ( Game1 ) , Program . gamePtr , "multiplayer" ) ;
//List<Server> servers = Helper.Reflection.GetField<List<Server>>(Game1.server, "servers", true).GetValue();
NetCode . GameServer server = new NetCode . GameServer ( ) ;
Game1 . server . stopServer ( ) ;
Game1 . server = server ;
server . startServer ( ) ;
serverHack = true ;
}
2018-08-21 04:53:56 +08:00
if ( Game1 . client ! = null & & serverHack = = false & & config . enableMultiplayerHack )
2018-08-08 15:12:31 +08:00
{
2018-08-08 01:07:33 +08:00
2018-08-08 15:12:31 +08:00
}
2018-08-08 01:07:33 +08:00
}
private void ControlEvents_KeyPressed ( object sender , StardewModdingAPI . Events . EventArgsKeyPressed e )
{
2018-08-23 16:23:42 +08:00
if ( e . KeyPressed . ToString ( ) = = config . modularMenuKey & & Game1 . activeClickableMenu = = null )
{
Game1 . activeClickableMenu = new ModularGameMenu ( 0 ) ;
}
2018-08-08 01:07:33 +08:00
}
2017-11-25 05:12:36 +08:00
2017-09-14 04:53:12 +08:00
private void SaveEvents_AfterLoad ( object sender , EventArgs e )
{
2018-08-08 01:07:33 +08:00
2017-09-14 04:53:12 +08:00
SerializationManager . restoreAllModObjects ( SerializationManager . trackedObjectList ) ;
2018-08-09 01:44:44 +08:00
//List<KeyValuePair<Vector2, MultiTileComponent>> objs = new List<KeyValuePair<Vector2, MultiTileComponent>>();
2018-08-07 05:01:44 +08:00
/ *
2018-06-14 02:11:56 +08:00
MultiTileComponent tile1 = new MultiTileComponent ( 0 , "Tileobj1" , "A basic tile obj" , new Texture2DExtended ( ModCore . ModHelper , Path . Combine ( "Content" , "Graphics" , "MultiTest" , "Test1.png" ) ) ) ;
MultiTileComponent tile2 = new MultiTileComponent ( 0 , "Tileobj2" , "A basic tile obj" , new Texture2DExtended ( ModCore . ModHelper , Path . Combine ( "Content" , "Graphics" , "MultiTest" , "Test2.png" ) ) ) ;
MultiTileComponent tile3 = new MultiTileComponent ( 0 , "Tileobj3" , "A basic tile obj" , new Texture2DExtended ( ModCore . ModHelper , Path . Combine ( "Content" , "Graphics" , "MultiTest" , "Test3.png" ) ) ) ;
2018-06-13 17:36:52 +08:00
objs . Add ( new KeyValuePair < Vector2 , MultiTileComponent > ( new Vector2 ( 0 , 0 ) , tile1 ) ) ;
objs . Add ( new KeyValuePair < Vector2 , MultiTileComponent > ( new Vector2 ( 1 , 0 ) , tile2 ) ) ;
objs . Add ( new KeyValuePair < Vector2 , MultiTileComponent > ( new Vector2 ( 2 , 0 ) , tile3 ) ) ;
2018-06-13 14:25:36 +08:00
2018-06-13 17:36:52 +08:00
MultiTileObject collection = new MultiTileObject ( "MultiTest" , "Trying to get multi object testing working" , Vector2 . Zero , new Texture2DExtended ( ModCore . ModHelper , Path . Combine ( "Content" , "Graphics" , "MultiTest" , "Test3.png" ) ) , objs , StardustCore . IlluminateFramework . Colors . invertColor ( StardustCore . IlluminateFramework . LightColorsList . Purple ) , "MultiTest" ) ;
2018-08-07 05:01:44 +08:00
* /
2018-06-13 14:25:36 +08:00
2018-08-07 05:01:44 +08:00
// Game1.player.addItemToInventory(collection);
2018-08-09 04:18:51 +08:00
/ *
2018-08-08 01:07:33 +08:00
CoreObject tile1 = new CoreObject ( new Texture2DExtended ( ModCore . ModHelper , Path . Combine ( "Content" , "Graphics" , "MultiTest" , "Test1.png" ) ) , 3 , Vector2 . Zero , 9 ) ;
2018-08-08 15:12:31 +08:00
2018-08-07 05:01:44 +08:00
tile1 . description = "Hello" ;
tile1 . Name = "test" ;
tile1 . displayName = "test" ;
Game1 . player . addItemToInventory ( tile1 ) ;
2018-08-09 04:18:51 +08:00
* /
2018-08-07 11:01:59 +08:00
2017-09-14 04:53:12 +08:00
}
private void SaveEvents_AfterSave ( object sender , EventArgs e )
{
SerializationManager . restoreAllModObjects ( SerializationManager . trackedObjectList ) ;
}
private void SaveEvents_BeforeSave ( object sender , EventArgs e )
{
SerializationManager . cleanUpInventory ( ) ;
SerializationManager . cleanUpWorld ( ) ;
2017-09-14 08:11:05 +08:00
SerializationManager . cleanUpStorageContainers ( ) ;
2017-09-13 02:20:18 +08:00
}
private void ControlEvents_MouseChanged ( object sender , StardewModdingAPI . Events . EventArgsMouseStateChanged e )
{
2018-08-23 16:23:42 +08:00
//???
return ;
2017-09-13 02:20:18 +08:00
if ( Game1 . activeClickableMenu = = null ) return ;
var MouseState = Mouse . GetState ( ) ;
if ( Game1 . activeClickableMenu is StardewValley . Menus . ItemGrabMenu & & MouseState . LeftButton = = ButtonState . Released )
{
( Game1 . activeClickableMenu as StardewValley . Menus . ItemGrabMenu ) . ItemsToGrabMenu . populateClickableComponentList ( ) ;
for ( int index = 0 ; index < ( Game1 . activeClickableMenu as StardewValley . Menus . ItemGrabMenu ) . ItemsToGrabMenu . inventory . Count ; + + index )
{
if ( ( Game1 . activeClickableMenu as StardewValley . Menus . ItemGrabMenu ) . ItemsToGrabMenu . inventory [ index ] ! = null )
{
( Game1 . activeClickableMenu as StardewValley . Menus . ItemGrabMenu ) . ItemsToGrabMenu . inventory [ index ] . myID + = 53910 ;
( Game1 . activeClickableMenu as StardewValley . Menus . ItemGrabMenu ) . ItemsToGrabMenu . inventory [ index ] . upNeighborID + = 53910 ;
( Game1 . activeClickableMenu as StardewValley . Menus . ItemGrabMenu ) . ItemsToGrabMenu . inventory [ index ] . rightNeighborID + = 53910 ;
( Game1 . activeClickableMenu as StardewValley . Menus . ItemGrabMenu ) . ItemsToGrabMenu . inventory [ index ] . downNeighborID = - 7777 ;
( Game1 . activeClickableMenu as StardewValley . Menus . ItemGrabMenu ) . ItemsToGrabMenu . inventory [ index ] . leftNeighborID + = 53910 ;
( Game1 . activeClickableMenu as StardewValley . Menus . ItemGrabMenu ) . ItemsToGrabMenu . inventory [ index ] . fullyImmutable = true ;
}
}
// (Game1.activeClickableMenu as ItemGrabMenu).inventory.playerInventory = false;
// Game1.activeClickableMenu =Game1.activeClickableMenu;//new ItemGrabMenu((Game1.activeClickableMenu as ItemGrabMenu).ItemsToGrabMenu.actualInventory,true,true,null,null,null,null,false,false,true,true,true,1,null,-1,null);
}
2017-09-12 09:35:31 +08:00
}
2017-07-14 11:27:48 +08:00
}
}