Test event callback code

This commit is contained in:
ClxS
2016-03-09 16:38:12 +00:00
parent 318f5feada
commit 27bc612a46
6 changed files with 161 additions and 8 deletions
+36
View File
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("SMAPIEvents")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("SMAPIEvents")]
[assembly: AssemblyCopyright("Copyright © 2016")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("02cd7f15-e611-4717-95ad-a798d34d0f19")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
+56
View File
@@ -0,0 +1,56 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{02CD7F15-E611-4717-95AD-A798D34D0F19}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>StardewModdingAPI</RootNamespace>
<AssemblyName>SMAPIEvents</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>..\StardewModdingAPI\bin\x86\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="Events\" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
+14
View File
@@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TrainerMod", "TrainerMod\Tr
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StardewModdingAPI", "StardewModdingAPI\StardewModdingAPI.csproj", "{F1A573B0-F436-472C-AE29-0B91EA6B9F8F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SMAPIEvents", "SMAPIEvents\SMAPIEvents.csproj", "{02CD7F15-E611-4717-95AD-A798D34D0F19}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -39,6 +41,18 @@ Global
{F1A573B0-F436-472C-AE29-0B91EA6B9F8F}.Release|Mixed Platforms.Build.0 = Release|x86
{F1A573B0-F436-472C-AE29-0B91EA6B9F8F}.Release|x86.ActiveCfg = Release|x86
{F1A573B0-F436-472C-AE29-0B91EA6B9F8F}.Release|x86.Build.0 = Release|x86
{02CD7F15-E611-4717-95AD-A798D34D0F19}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{02CD7F15-E611-4717-95AD-A798D34D0F19}.Debug|Any CPU.Build.0 = Debug|Any CPU
{02CD7F15-E611-4717-95AD-A798D34D0F19}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{02CD7F15-E611-4717-95AD-A798D34D0F19}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{02CD7F15-E611-4717-95AD-A798D34D0F19}.Debug|x86.ActiveCfg = Debug|Any CPU
{02CD7F15-E611-4717-95AD-A798D34D0F19}.Debug|x86.Build.0 = Debug|Any CPU
{02CD7F15-E611-4717-95AD-A798D34D0F19}.Release|Any CPU.ActiveCfg = Release|Any CPU
{02CD7F15-E611-4717-95AD-A798D34D0F19}.Release|Any CPU.Build.0 = Release|Any CPU
{02CD7F15-E611-4717-95AD-A798D34D0F19}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{02CD7F15-E611-4717-95AD-A798D34D0F19}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{02CD7F15-E611-4717-95AD-A798D34D0F19}.Release|x86.ActiveCfg = Release|Any CPU
{02CD7F15-E611-4717-95AD-A798D34D0F19}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
+16
View File
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StardewModdingAPI.Events
{
public class FarmAnimal
{
public void eatGrass_OnEnter()
{
}
}
}
+38 -8
View File
@@ -1,6 +1,7 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Mono.Cecil;
using Mono.Cecil.Cil;
using StardewModdingAPI.Events;
using StardewModdingAPI.Inheritance;
using StardewModdingAPI.Inheritance.Menus;
@@ -52,7 +53,7 @@ namespace StardewModdingAPI
ConfigurePaths();
ConfigureMethodInjection();
ConfigureInjector();
ConfigureSDV();
//ConfigureSDV();
//GameRunInvoker();
}
@@ -71,13 +72,42 @@ namespace StardewModdingAPI
/// </summary>
private static void ConfigureMethodInjection()
{
AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly(Constants.ExecutionPath + "\\Stardew Valley.exe");
TypeDefinition type = assembly.MainModule.Types.FirstOrDefault(n => n.FullName == "StardewValley.FarmAnimal");
//if (type != null)
//{
MethodDefinition foundMethod = type.Methods.FirstOrDefault(m => m.Name == "eatGrass");
Mono.Cecil.Cil.ILProcessor worker = foundMethod.Body.GetILProcessor();
//}
AssemblyDefinition stardewAssembly = AssemblyDefinition.ReadAssembly(Constants.ExecutionPath + "\\Stardew Valley.exe");
TypeDefinition type = stardewAssembly.MainModule.Types.FirstOrDefault(n => n.FullName == "StardewValley.FarmAnimal");
MethodDefinition foundMethod = type.Methods.FirstOrDefault(m => m.Name == "eatGrass");
Mono.Cecil.Cil.ILProcessor ilProcessor = foundMethod.Body.GetILProcessor();
var smapiAssembly = Assembly.GetExecutingAssembly().GetType("StardewModdingAPI.Events.FarmAnimal");
var eventMethod = smapiAssembly.GetMethod("eatGrass_OnEnter");
AssemblyDefinition fstardewAssembly = AssemblyDefinition.ReadAssembly(Assembly.GetExecutingAssembly().Location);
MethodReference ffoundMethod = stardewAssembly.MainModule.Import(eventMethod);
WeaveOnEnterMethod(ilProcessor, foundMethod.Body.Instructions.First(), ffoundMethod);
WeaveOnExitMethod(ilProcessor, foundMethod.Body.Instructions.Where(i => i.OpCode == OpCodes.Ret).ToList(), ffoundMethod);
MemoryStream stream = new MemoryStream();
stardewAssembly.Write(stream);
Assembly.Load(stream.GetBuffer());
}
private static void WeaveOnEnterMethod(Mono.Cecil.Cil.ILProcessor ilProcessor, Instruction target, MethodReference callback)
{
// Instruction loadNameInstruction = ilProcessor.Create(OpCodes.Ldstr, name);
//ilProcessor.InsertBefore(target, loadNameInstruction);
Instruction callEnterInstruction = ilProcessor.Create(OpCodes.Call, callback);
ilProcessor.InsertBefore(target, callEnterInstruction);
}
private static void WeaveOnExitMethod(Mono.Cecil.Cil.ILProcessor ilProcessor, List<Instruction> targets, MethodReference callback)
{
// Instruction loadNameInstruction = ilProcessor.Create(OpCodes.Ldstr, name);
//ilProcessor.InsertBefore(target, loadNameInstruction);
foreach (var returnInstruction in targets)
{
Instruction callEnterInstruction = ilProcessor.Create(OpCodes.Call, callback);
ilProcessor.InsertBefore(returnInstruction, callEnterInstruction);
}
}
/// <summary>
@@ -134,6 +134,7 @@
<Compile Include="Entities\SPlayer.cs" />
<Compile Include="Events\Controls.cs" />
<Compile Include="Events\EventArgs.cs" />
<Compile Include="Events\FarmAnimal.cs" />
<Compile Include="Events\Game.cs" />
<Compile Include="Events\Graphics.cs" />
<Compile Include="Events\Location.cs" />