﻿using System;
using HarmonyLib;

namespace undead_universal_patch.Patches
{
    [HarmonyPatch(typeof(UnityEngine.Resources), nameof(UnityEngine.Resources.Load), [ typeof(string), typeof(Type) ])]
    class Resource
    {
        static bool Ran = false;
        [HarmonyPrepare]
        static bool Prepare()
        {
            // Improves performance over the previous version
            // This is actually untested. (The performance claim and this Prepare method.)
            return PhotonConfig.PatchPhotonIds.Value && PhotonConfig.PatchEvent.Value == "Load";
        }
        [HarmonyPostfix]
        static void Postfix(ref string path, ref object __result)
        {
            //Plugin.Log.LogInfo($"Resource loading '{path}'"); // uncomment to log every loaded resource
            if (path.Contains("PhotonServerSettings"))
            {
                if (Ran) return; // Photon.Patch calls Resources.Load, which may result in a loop if not handled properly
                Ran = true;
                __result = Photon.Patch();
            }
        }
    }
}
