Verify Model Integrity
Updated: 03/26/2024
Before loading a model, the Cubism SDK can verify that the model does not contain incorrect data due to tampering.
If unspecified MOC3 files are expected to be loaded, it is recommended that the integrity of the MOC3 files to be loaded be checked before generating csmMoc objects with csmInitializeModelInPlace.
Note, however, that integrity verification may affect performance.
To verify model integrity, use Cubism Core’s csmHasMocConsistency()
( Live2DCubismCore.hasMocConsistency()
for SDK for Java).
See “Cubism Core API Reference” for details.
CubismMoc::Create()
in Cubism SDK for Native, CubismMoc.create()
in Cubism SDK for Web, and CubismMoc.create()
in Cubism SDK for Java verify MOC3 file integrity when loading models.
// Cubism SDK for Native CubismMoc* CubismMoc::Create(const csmByte* mocBytes, csmSizeInt size, csmBool shouldCheckMocConsistency) { CubismMoc* cubismMoc = NULL; void* alignedBuffer = CSM_MALLOC_ALLIGNED(size, Core::csmAlignofMoc); memcpy(alignedBuffer, mocBytes, size); if (shouldCheckMocConsistency) { // Check integrity of .moc3. csmBool consistency = HasMocConsistency(alignedBuffer, size); if (!consistency) { CSM_FREE_ALLIGNED(alignedBuffer); // If integrity cannot be confirmed, do not process. CubismLogError("Inconsistent MOC3."); return cubismMoc; } } ... return cubismMoc; }
// Cubism SDK for Web public static create( mocBytes: ArrayBuffer, shouldCheckMocConsistency: boolean ): CubismMoc { let cubismMoc: CubismMoc = null; if (shouldCheckMocConsistency) { // Check integrity of .moc3. const consistency = this.hasMocConsistency(mocBytes); if (!consistency) { // If integrity cannot be confirmed, do not process. CubismLogError(`Inconsistent MOC3.`); return cubismMoc; } } ... return cubismMoc; }
// Cubism SDK for Java public static CubismMoc create(byte[] mocBytes, boolean shouldCheckMocConsistency) { com.live2d.sdk.cubism.core.CubismMoc moc; if (mocBytes == null) { return null; } if (shouldCheckMocConsistency) { // Check integrity of .moc3. boolean consistency = hasMocConsistency(mocBytes); if (!consistency) { CubismDebug.cubismLogError("Inconsistent MOC3."); return null; } } ... return cubismMoc; }
The Cubism SDK sample performs integrity verification when loading by default, but this can be optionally disabled.
To disable integrity verification when loading models, change the value of LAppDefine::MocConsistencyValidationEnable
in SDK for Native, LAppDefine.MOCConsistencyValidationEnable
in SDK for Web, and LAppDefine.MOC_CONSISTENCY_VALIDATION_ENABLE
in SDK for Java.
// Cubism SDK for Native // MOC3 integrity verification options const csmBool MocConsistencyValidationEnable = true;
// Cubism SDK for Web // MOC3 integrity verification options export const MOCConsistencyValidationEnable = true;
// Cubism SDK for Java /** * Whether to verify the integrity of MOC3 files. True if valid. */ public static final boolean MOC_CONSISTENCY_VALIDATION_ENABLE = true;
Similarly, Unity verifies integrity on model import with CubismMoc.CreateFrom()
by default.
// Cubism SDK for Unity public static CubismMoc CreateFrom(byte[] moc3, bool shouldCheckMocConsistency = true) { if (shouldCheckMocConsistency && !HasMocConsistency(moc3)) { Debug.LogError("This Moc3 is Invalid. This model generation process is aborted and prefab is not created.\n" + $"Please check Model's Moc version. This CubismCore supported latest Moc version is `{LatestVersion}`."); return null; } var moc = CreateInstance<CubismMoc>(); moc.Bytes = moc3; return moc; }