Skip to content

Commit

Permalink
Skybox support, Reflection caching, Author...
Browse files Browse the repository at this point in the history
Offmesh link parameter change, unitypackage 1.1.0.0
  • Loading branch information
Nolenz committed Jun 17, 2020
1 parent ac816ea commit fd9bdfa
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 23 deletions.
6 changes: 6 additions & 0 deletions WurstMod/Exporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public static void ExportBundle()
if (!choice) return;
}

// Pre-save, grab the skybox.
levelComponent.skybox = RenderSettings.skybox;

if (EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo())
{
// Setup build options.
Expand Down Expand Up @@ -110,10 +113,13 @@ public static string Validate(Scene scene, List<string> warnings)
return "You must have at least two Hold Points.";
}
// Hold points must have at least 9 defenders spawnpoints.
// NavBlockers must be disabled.
foreach (TNH.TNH_HoldPoint hold in holds)
{
if (hold.SpawnPoints_Sosigs_Defense.AsEnumerable().Count() < 9)
return "All holds must have at least 9 entries in SpawnPoints_Sosigs_Defense.";
if (hold.NavBlockers.activeSelf)
return "All NavBlockers must be disabled before exporting.";
}

// UNVERIFIED Must have at least 3 Supply Points.
Expand Down
60 changes: 56 additions & 4 deletions WurstMod/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,70 @@ public static IEnumerable<Transform> AsEnumerable(this Transform transform)
#endregion

#region Reflection
// Reflection shortcuts.
// Reflection shortcuts with caching. A smidge less verbose than InvokeMember.
// Currently doesn't work for overloaded methods.
// Cross that bridge when we come to it, I guess.
private struct ReflectDef
{
public Type type;
public string name;
public ReflectDef(Type type, string name)
{
this.type = type;
this.name = name;
}
}
private static Dictionary<ReflectDef, MethodInfo> methodCache = new Dictionary<ReflectDef, MethodInfo>();
private static Dictionary<ReflectDef, FieldInfo> fieldCache = new Dictionary<ReflectDef, FieldInfo>();

public static object ReflectInvoke<T>(this T target, string methodName, params object[] parameters) where T : UnityEngine.Object
{
MethodInfo method = target.GetType().GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance);
MethodInfo method;
ReflectDef def = new ReflectDef(target.GetType(), methodName);
if (methodCache.ContainsKey(def))
{
method = methodCache[def];
}
else
{
method = target.GetType().GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance);
methodCache[def] = method;
}

return method.Invoke(target, parameters);
}

public static object ReflectGet<T>(this T target, string fieldName) where T : UnityEngine.Object
{
FieldInfo field = target.GetType().GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance);
FieldInfo field;
ReflectDef def = new ReflectDef(target.GetType(), fieldName);
if (fieldCache.ContainsKey(def))
{
field = fieldCache[def];
}
else
{
field = target.GetType().GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance);
fieldCache[def] = field;
}

return field.GetValue(target);
}

public static void ReflectSet<T>(this T target, string fieldName, object value) where T : UnityEngine.Object
{
FieldInfo field = target.GetType().GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance);
FieldInfo field;
ReflectDef def = new ReflectDef(target.GetType(), fieldName);
if (fieldCache.ContainsKey(def))
{
field = fieldCache[def];
}
else
{
field = target.GetType().GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance);
fieldCache[def] = field;
}

field.SetValue(target, value);
}

Expand All @@ -66,6 +113,11 @@ public static List<GameObject> GetAllGameObjectsInScene(this Scene scene)
}
return allGameObjects;
}

public static void RefreshShader(this Material mat)
{
mat.shader = Shader.Find(mat.shader.name);
}
#endregion

#region Gizmos
Expand Down
11 changes: 10 additions & 1 deletion WurstMod/LevelSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public static void SetupLevelSelector(Scene loaded)
currentLevelIndex = 0;
GatherReferences();
InitDirectories();
FixLevelNameSize();
SetupLevelDatas();

SetupButtons();
Expand Down Expand Up @@ -79,12 +80,20 @@ private static void InitDirectories()
}
}

private static void FixLevelNameSize()
{
RectTransform levelNameRect = levelNameText.transform as RectTransform;
levelNameRect.sizeDelta = new Vector2(860, 80);
}

/// <summary>
/// Create our own LevelData objects for the original level, and all loaded levels.
/// </summary>
private static void SetupLevelDatas()
{
levels.Add(new LevelData(levelImage.sprite, levelNameText.text, "H3VR", levelDescriptionText.text, ""));
levels.Add(new LevelData(levelImage.sprite, "Classic", "H3VR", levelDescriptionText.text, ""));
//TODO TEST THIS Update information of original level for consistency with new format.
levels[0].SetLevel();

foreach(string ii in Directory.GetDirectories(levelDir))
{
Expand Down
33 changes: 21 additions & 12 deletions WurstMod/Loader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ private static void SetWhitelistedStates(bool state)
static FistVR.AudioEvent success;
static FistVR.AudioEvent failure;
static GameObject vfx;
static Shader shader;
static GameObject[] barrierPrefabs = new GameObject[2];
/// <summary>
/// A wide variety of existing objects are needed for importing a new TNH scene.
Expand Down Expand Up @@ -174,11 +173,6 @@ private static void CollectRequiredObjects()
// We need VFX_HoldWave prefab.
vfx = sourceHoldPoint.VFX_HoldWave;

// We need the correct shader for hotswapping.
//TODO this is terrible, but I can't fix the right eye bug...
MeshRenderer mr = currentScene.GetAllGameObjectsInScene().Where(x => x.name == "SciHalls_Support_A").First().GetComponent<MeshRenderer>();
shader = mr.materials[0].shader;

// We need barrier prefabs.
FistVR.TNH_DestructibleBarrierPoint barrier = currentScene.GetAllGameObjectsInScene().Where(x => x.name == "Barrier_SpawnPoint").First().GetComponent<FistVR.TNH_DestructibleBarrierPoint>();
barrierPrefabs[0] = barrier.BarrierDataSets[0].BarrierPrefab;
Expand Down Expand Up @@ -243,6 +237,7 @@ private static IEnumerator MergeInScene()
/// </summary>
private static void ResolveAll()
{
Resolve_Skybox();
Resolve_Shaders();
Resolve_PMats();
Resolve_FVRReverbEnvironments();
Expand All @@ -258,18 +253,33 @@ private static void ResolveAll()

#region Resolves
/// <summary>
/// Steal a properly-formatted shader from an existing object.
/// This is required because using any other shader seems to break VR (at least in one eye).
/// I'm not sure what the deal is with this. I'm sure there's a solution, but for now
/// everything is going to have to have a simple shader.
/// Use the skybox of the imported level.
/// Requires GI Update to fix lighting.
/// </summary>
private static void Resolve_Skybox()
{
TNH.TNH_Level levelComponent = loadedRoot.GetComponent<TNH.TNH_Level>();
if (levelComponent.skybox != null)
{
RenderSettings.skybox = levelComponent.skybox;
RenderSettings.skybox.RefreshShader();
DynamicGI.UpdateEnvironment();
}
}


/// <summary>
/// Shaders, when imported from an assetbundle, become garbage.
/// Set them to themselves and bam, it works.
/// Unity 5 bugs sure were something.
/// </summary>
private static void Resolve_Shaders()
{
foreach (MeshRenderer ii in loadedRoot.GetComponentsInChildren<MeshRenderer>(true))
{
foreach (Material jj in ii.materials)
{
jj.shader = shader;
jj.RefreshShader();
}
}
}
Expand Down Expand Up @@ -328,7 +338,6 @@ private static void Resolve_FVRHandGrabPoints()
Collider proxyCol = proxy.GetComponent<Collider>();
Vector3 extents = proxyCol.bounds.extents;
real.EndInteractionDistance = 2.5f * Mathf.Abs(Mathf.Max(extents.x, extents.y, extents.z));
Debug.Log("INTERACTION DISTANCE: " + real.EndInteractionDistance);
}
}

Expand Down
8 changes: 2 additions & 6 deletions WurstMod/Patches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ static bool Prefix(Sosig __instance)

// Force fields on the fake link to match the type of jump.
Vector3 jump = __instance.Agent.currentOffMeshLinkData.endPos - __instance.Agent.currentOffMeshLinkData.startPos;
float totalMag = jump.magnitude;
float horzMag = new Vector3(jump.x, 0, jump.z).magnitude;
float vertMag = Mathf.Abs(jump.y);
bool jumpDown = jump.y < 0;
Expand All @@ -83,11 +82,8 @@ static bool Prefix(Sosig __instance)
else if (jumpDown && vertMag > (2 * horzMag)) fakeLink.Type = NavMeshLinkExtension.NavMeshLinkType.Drop;
else fakeLink.Type = NavMeshLinkExtension.NavMeshLinkType.LateralJump;

// TODO These numbers may require more tweaking.
if (fakeLink.Type == NavMeshLinkExtension.NavMeshLinkType.Climb) fakeLink.TimeToClear = 40f;
else fakeLink.TimeToClear = 40f;

fakeLink.ReflectSet("m_xySpeed", totalMag / fakeLink.TimeToClear);
// Arbitrary number.
fakeLink.ReflectSet("m_xySpeed", 0.5f);


// Clean up the dictionary. Keep an eye on the counts for this...
Expand Down
2 changes: 2 additions & 0 deletions WurstMod/TNH/TNH_Level.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ public class TNH_Level : MonoBehaviour

[TextArea(15, 20)]
public string levelDescription;

public Material skybox;
}
}
Binary file modified WurstModWorkbench.unitypackage
Binary file not shown.

0 comments on commit fd9bdfa

Please sign in to comment.