Skip to content
This repository has been archived by the owner on Jan 14, 2023. It is now read-only.

Commit

Permalink
Fix some crashes, improve code consistency, reduce log spam.
Browse files Browse the repository at this point in the history
  • Loading branch information
yretenai committed Jan 8, 2022
1 parent 56218df commit 74a7bad
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Snuggle.Core/Implementations/PlayerSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ public PlayerSettings(BiEndianBinaryReader reader, UnityObjectInfo info, Seriali
}

if (SerializedFile.Version >= UnityVersionRegister.Unity2019_4) {
if (SerializedFile.Options.Game != UnityGame.PokemonUnite || SerializedFile.Options.GameOptions.TryGetOptionsObject<UniteOptions>(UnityGame.PokemonUnite, out var uniteOptions) && uniteOptions.GameVersion >= UniteVersion.Version1_2) {
if (SerializedFile.Options.Game is not UnityGame.PokemonUnite || SerializedFile.Options.GameOptions.TryGetOptionsObject<UniteOptions>(UnityGame.PokemonUnite, out var uniteOptions) && uniteOptions.GameVersion >= UniteVersion.Version1_2) {
StadiaPresentMode = reader.ReadInt32();
StadiaTargetFramerate = reader.ReadInt32();
}
Expand Down
16 changes: 8 additions & 8 deletions Snuggle.Core/Implementations/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ public Renderer(BiEndianBinaryReader reader, UnityObjectInfo info, SerializedFil
reader.Align();
}

CastShadows = reader.ReadByte();
RecieveShadows = reader.ReadByte();
CastShadows = reader.ReadBoolean();
RecieveShadows = reader.ReadBoolean();
if (SerializedFile.Version >= UnityVersionRegister.Unity2017_2) {
DynamicOccludee = reader.ReadByte();
DynamicOccludee = reader.ReadBoolean();
}

if (SerializedFile.Version >= UnityVersionRegister.Unity2021_1) {
StaticShadowCaster = reader.ReadByte();
StaticShadowCaster = reader.ReadBoolean();
}

if (SerializedFile.Version >= UnityVersionRegister.Unity5_4) {
Expand Down Expand Up @@ -100,10 +100,10 @@ public Renderer(UnityObjectInfo info, SerializedFile serializedFile) : base(info
LightProbeVolumeOverride = PPtr<GameObject>.Null;
}

public byte CastShadows { get; set; }
public byte RecieveShadows { get; set; }
public byte DynamicOccludee { get; set; }
public byte StaticShadowCaster { get; set; }
public bool CastShadows { get; set; }
public bool RecieveShadows { get; set; }
public bool DynamicOccludee { get; set; }
public bool StaticShadowCaster { get; set; }
public byte MotionVectors { get; set; }
public byte LightProbeUsage { get; set; }
public byte RayTracingMode { get; set; }
Expand Down
6 changes: 3 additions & 3 deletions Snuggle.Core/ObjectFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ private static bool TryFindObjectType(UnityObjectInfo info, SerializedFile seria

hasImplementation = gameImplementations.TryGetValue(overrideType ?? info.ClassId, out type);
if (!hasImplementation) {
if (overrideGame != UnityGame.Default) {
if (overrideGame is not UnityGame.Default) {
overrideGame = UnityGame.Default;
return false;
}
Expand All @@ -137,7 +137,7 @@ private static bool TryFindObjectType(UnityObjectInfo info, SerializedFile seria

type ??= BaseType;

if (overrideGame == UnityGame.Default && serializedFile.Options.Game != UnityGame.Default && DisabledDefaultImplementations.TryGetValue(overrideType ?? info.ClassId, out var disabledGames) && disabledGames.Contains(serializedFile.Options.Game)) {
if (overrideGame is UnityGame.Default && serializedFile.Options.Game is not UnityGame.Default && DisabledDefaultImplementations.TryGetValue(overrideType ?? info.ClassId, out var disabledGames) && disabledGames.Contains(serializedFile.Options.Game)) {
type = BaseType;
}

Expand Down Expand Up @@ -167,11 +167,11 @@ private static SerializedObject CreateObjectInstance(
if (memoryUse >= 1.ToMebiByte()) {
serializedFile.Options.Logger.Warning("Object", $"Using {memoryUse.GetHumanReadableBytes()} of memory to load object {info.PathId} ({overrideType ?? info.ClassId:G}, {overrideGame ?? UnityGame.Default:G}), consider moving some things to ToSerialize()");
}
#endif

if (overrideType == null && hasImplementation && reader.Unconsumed > 0 && !serializedObject.ShouldDeserialize) {
serializedFile.Options.Logger.Warning("Object", $"{reader.Unconsumed} bytes left unconsumed in buffer and object {info.PathId} ({overrideType ?? info.ClassId:G}, {overrideGame ?? UnityGame.Default:G}) is not marked for deserialization! Check implementation");
}
#endif

if (serializedObject is ISerializedResource resource) {
serializedObject.Size += resource.StreamData.Size;
Expand Down
2 changes: 1 addition & 1 deletion Snuggle.Headless/GameFlags/GameFlagsAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ public class GameFlagsAttribute : Attribute {
public UnityGame Game { get; }

public override object TypeId => (int) Game;
public override bool IsDefaultAttribute() => Game == UnityGame.Default;
public override bool IsDefaultAttribute() => Game is UnityGame.Default;
}
6 changes: 3 additions & 3 deletions Snuggle.Headless/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static int Main() {
logger.Debug("System", flags.ToString());
logger.Debug("System", $"Args: {string.Join(' ', Environment.GetCommandLineArgs()[1..])}");
IGameFlags? gameFlags = null;
if (flags.Game != UnityGame.Default && additionalFlags.TryGetValue(flags.Game, out var additionalGameFlags)) {
if (flags.Game is not UnityGame.Default && additionalFlags.TryGetValue(flags.Game, out var additionalGameFlags)) {
gameFlags = CommandLineFlags.ParseFlags(additionalGameFlags) as IGameFlags;
if (gameFlags != null) {
logger.Debug("System", gameFlags.ToString());
Expand Down Expand Up @@ -77,7 +77,7 @@ public static int Main() {

var collection = new AssetCollection();
var options = SnuggleCoreOptions.Default with { Game = flags.Game, Logger = logger, CacheDataIfLZMA = true, IgnoreClassIds = flags.IgnoreClassIds };
if (options.Game != UnityGame.Default && gameFlags != default) {
if (options.Game is not UnityGame.Default && gameFlags != default) {
options.GameOptions.StorageMap[options.Game] = JsonSerializer.SerializeToElement(gameFlags.ToOptions(), SnuggleCoreOptions.JsonOptions);
}

Expand Down Expand Up @@ -157,7 +157,7 @@ public static int Main() {
}
}

if (options.Game != UnityGame.Default && options.GameOptions.StorageMap.ContainsKey(options.Game)) {
if (options.Game is not UnityGame.Default && options.GameOptions.StorageMap.ContainsKey(options.Game)) {
logger.Info("System", "Updated Game Settings");
var jsonOptions = new JsonSerializerOptions(SnuggleCoreOptions.JsonOptions) { WriteIndented = false };
logger.Info("System", JsonSerializer.Serialize(options.GameOptions.StorageMap[options.Game], jsonOptions));
Expand Down
5 changes: 4 additions & 1 deletion Snuggle/Converters/MeshToHelixConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Media;
using System.Windows.Media.Media3D;
Expand All @@ -26,6 +27,8 @@
namespace Snuggle.Converters;

public static class MeshToHelixConverter {
private static readonly Regex XAMLSafeCharacters = new("[^a-zA-Z0-9_]", RegexOptions.Compiled);

private static List<Object3D> GetSubmeshes(Mesh mesh, CancellationToken token) {
if (mesh.ShouldDeserialize) {
throw new IncompleteDeserialization();
Expand Down Expand Up @@ -319,7 +322,7 @@ private static unsafe void BuildSubmeshes(ICollection<Element3D> collection, IRe
RenderWireframe = SnuggleCore.Instance.Settings.MeshExportOptions.DisplayWireframe,
WireframeColor = Colors.Red,
Geometry = submesh.Geometry,
Name = submesh.Name.Replace('.', '_'),
Name = XAMLSafeCharacters.Replace(submesh.Name, "_"),
Material = material3d,
Transform = Transform3D.Identity,
});
Expand Down

0 comments on commit 74a7bad

Please sign in to comment.