验证模型一致性

最終更新: 2024年3月26日

Cubism SDK可以在导入模型前验证模型不包含因篡改而导致的无效数据。

如果要导入未指定的MOC3文件,建议在使用csmInitializeModelInPlace生成csmMoc物体前确认要导入的MOC3文件的一致性。
但是,请注意一致性验证可能会影响性能。

对于模型一致性验证,请使用Cubism Core的 csmHasMocConsistency()(SDK for Java为 Live2DCubismCore.hasMocConsistency())。
有关详细信息,请参考“Cubism Core API参考”。

Cubism SDK for Native的 CubismMoc::Create() 、Cubism SDK for Web的 CubismMoc.create() 和Cubism SDK for Java的 CubismMoc.create() 在导入模型时验证MOC3文件的一致性。

// 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)
    {
        // 确认.moc3的一致性
        csmBool consistency = HasMocConsistency(alignedBuffer, size);
        if (!consistency)
        {
            CSM_FREE_ALLIGNED(alignedBuffer);

            // 如果不能确认一致性则不处理
            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) {
      // 确认.moc3的一致性
      const consistency = this.hasMocConsistency(mocBytes);

      if (!consistency) {
        // 如果不能确认一致性则不处理
        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) {
            // 确认.moc3的一致性。
            boolean consistency = hasMocConsistency(mocBytes);

            if (!consistency) {
                CubismDebug.cubismLogError("Inconsistent MOC3.");
                return null;
            }
        }

        ...

        return cubismMoc;
    }


默认情况下,Cubism SDK范例在导入时执行一致性验证,但您可以选择禁用它。

要禁用模型导入一致性验证,请更改SDK for Native中的LAppDefine::MocConsistencyValidationEnable、SDK for Web中的LAppDefine.MOCConsistencyValidationEnable和SDK for Java中的LAppDefine.MOC_CONSISTENCY_VALIDATION_ENABLE的值。

// Cubism SDK for Native

    // MOC3一致性验证选项
    const csmBool MocConsistencyValidationEnable = true;
// Cubism SDK for Web

// MOC3一致性验证选项
export const MOCConsistencyValidationEnable = true;
// Cubism SDK for Java

    /**
     * 是否验证MOC3一致性。如果有效则为true。
     */
    public static final boolean MOC_CONSISTENCY_VALIDATION_ENABLE = true;


同样,Unity默认情况下会在使用CubismMoc.CreateFrom()载入模型时验证一致性。

// 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;
}
请问这篇文章对您有帮助吗?
关于本报道,敬请提出您的意见及要求。