EllanJiang/GameFramework · error · GameFrameworkException

Code is invalid.

Error message

Code is invalid.

What it means

Utility.GetCrc32 throws GameFrameworkException with 'Code is invalid.' when the byte[] code parameter passed to it is null. The code array is the seed value used for the CRC32 computation, so a null array makes the calculation impossible and the library refuses to proceed. This is an argument-null guard, not a runtime failure.

Solutions

  1. Pass a non-null byte array as the code argument (typically a 4-byte seed).
  2. Check for null before calling and supply a default seed such as new byte[] { 0, 0, 0, 0 }.
  3. Fix upstream code that produced a null seed (failed deserialization, unset config).

Example fix

// before
int crc = Utility.Verifier.GetCrc32(stream, code, length); // code is null
// after
byte[] code = codeBytes ?? new byte[] { 0, 0, 0, 0 };
int crc = Utility.Verifier.GetCrc32(stream, code, length);
Defensive patterns

Strategy: validation

Validate before calling

if (stream != null && code != null && code.Length > 0)
{
    int crc = Utility.Verifier.GetCrc32(stream, code, length);
}
else
{
    Log.Warning("GetCrc32 skipped: null stream or invalid code seed.");
}

Type guard

bool IsValidCrcSeed(byte[] code) => code != null && code.Length > 0;

Try / catch

try
{
    int crc = Utility.Verifier.GetCrc32(stream, code, length);
}
catch (GameFrameworkException ex) when (ex.Message == "Code is invalid." || ex.Message == "Stream is invalid." || ex.Message == "Code length is invalid.")
{
    Log.Error("CRC32 computation failed: {0}", ex.Message);
}

Prevention

When it happens

Trigger: Calling Utility.Verifier.GetCrc32(stream, code, length) with code == null (any stream state).

Common situations: A field or variable holding the CRC seed was never initialized; deserialized config returned null; a caller misread the API and thought the code parameter was optional.

Related errors


AI-assisted analysis of EllanJiang/GameFramework@d0c010b051 (2026-09-15). Data as JSON: /api/errors/d1f008fe0c0a6fff. Report an issue: GitHub.

Appendix: source

Thrown at GameFramework/Utility/Utility.Verifier.cs:148

                    throw new GameFrameworkException("Offset or length is invalid.");
                }

                bytes[offset] = (byte)((crc32 >> 24) & 0xff);
                bytes[offset + 1] = (byte)((crc32 >> 16) & 0xff);
                bytes[offset + 2] = (byte)((crc32 >> 8) & 0xff);
                bytes[offset + 3] = (byte)(crc32 & 0xff);
            }

            internal static int GetCrc32(Stream stream, byte[] code, int length)
            {
                if (stream == null)
                {
                    throw new GameFrameworkException("Stream is invalid.");
                }

                if (code == null)
                {
                    throw new GameFrameworkException("Code is invalid.");
                }

                int codeLength = code.Length;
                if (codeLength <= 0)
                {
                    throw new GameFrameworkException("Code length is invalid.");
                }

                int bytesLength = (int)stream.Length;
                if (length < 0 || length > bytesLength)
                {
                    length = bytesLength;
                }

                int codeIndex = 0;
                while (true)
                {
                    int bytesRead = stream.Read(s_CachedBytes, 0, CachedBytesLength);

View on GitHub (pinned to d0c010b051)