Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make entrypoint for other mods to add money #67

Closed
harbingerofme opened this issue Jan 6, 2020 · 3 comments
Closed

Make entrypoint for other mods to add money #67

harbingerofme opened this issue Jan 6, 2020 · 3 comments
Labels
enhancement New feature or request

Comments

@harbingerofme
Copy link
Contributor

Is your feature request related to a problem? Please describe.
Mods are currenunable to add money easily when moneysharing is enabled.

Describe the solution you'd like
A method in the main plugin class to add money to a charactermaster/body. This could be found using reflection so that other mods can make themselves compatible with sharesuites moneysharing.

Describe alternatives you've considered
The better solution would be changing how money is handled by sharesuite in the firdt place, but the first solution provides a bandaid fix.

Additional context
BiggerBazaar and Debugtoolkit are noteworthy incompatibilities.

@harbingerofme harbingerofme added the enhancement New feature or request label Jan 6, 2020
@harbingerofme
Copy link
Contributor Author

harbingerofme commented Jan 6, 2020

EDIT: I have since learned a better way, see my comment down below.

Such a method could be retrieved and cached by external plugins like so:

private MethodInfo AddMoney = null;
private BaseUnityPlugin ShareSuite = null;
private void Start(){
            if (Chainloader.PluginInfos.ContainsKey("com.funkfrog_sipondo.sharesuite"))
            {
                ShareSuite = Chainloader.PluginInfos["com.funkfrog_sipondo.sharesuite"].Instance;
                AddMoney = ShareSuite.GetType().GetMethod("AddMoneyExternal", BindingFlags.Instance | BindingFlags.Public);
            }
}

And then be called at any point later like so:

if(ShareSuite)
        AddMoney.Invoke(ShareSuite, new object[] { amount });

If an external plugin wants to call the chainloader during their constructor or their Awake, they will need to add

[BepInDependency("com.funkfrog_sipondo.sharesuite",BepInDependency.DependencyFlags.SoftDependency)]

To their plugin class. This ensures BepInEx loads sharesuite before them and thus the sharesuite instance exists.

@FunkFrog
Copy link
Owner

Added in 1.15.0. AddMoneyExternal in the MoneySharingHooks class.

@harbingerofme
Copy link
Contributor Author

To enable cross compatibility of your mod with ShareSuite, you will need to do the following things:

  • Reference the sharesuite.dll. You can acquire this from thunderstore.
  • Check if the chainloader has sharesuite loaded (alternatively, you can check the appdomain I suppose). You can do this during your startup.
  • When you want to add money, check in an additional method if sharesuite is running moneysharing hooks.
    • If so, add money in an additional method.

You must use those additional methods, because an end user might not have ShareSuite installed, so the types for it may not be in the AppDomain, causing the JIT to crash. You may combine those additional methods if you really want to, but make sure they aren't being tried to call whenever you are not sure that ShareSuite is currently available.

Here's an example:

using ShareSuite;

class myPlugin {
private bool hasShareSuite= false;

public void awake(){
    if (Chainloader.PluginInfos.ContainsKey("com.funkfrog_sipondo.sharesuite")) {
          this.hasShareSuite = true;
    }
}


void myMoneyAddingStuff(CharacterMaster target, int amount){
    if(this.hasShareSuite && checkUseShareSuite()){
        //maybe divide the amount of money gained by players in the game or something, who knows?
        giveMoneyShareSuite(amount);
        return;
     } else {
        target.GiveMoney(amount);
    }
}

[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
bool checkUseShareSuite(){
    return ShareSuite.ShareSuite.MoneyIsShared.Value;
}

[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
void ShareSuiteGive(int amount)
{
      ShareSuite.MoneySharingHooks.AddMoneyExternal(amount);
}

}

You'll notice I use [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)] to make double sure that the JIT doesn't try anything wonky.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants