Windows 8 Phone: Failed to run serialization weaver

Hello,

Trying to port one of our projects over to Windows8 Phone today, and sadly hitting an issue with the “serialization weaver”. I’m running Unity 4.3.4 on Windows 8.1. All my C# scripts compile clean, but I am getting the following build error. Nothing I’ve tried has improved results (including a re-install of Unity).

Any ideas on this one?

Here’s what I find in the Editor.log file:

    Error building Player: Exception: Failed to run serialization weaver with command line "Temp/StagingArea/Data/Managed\Assembly-CSharp.dll" -pdb -verbose -unity-engine=Temp/StagingArea/Data/Managed/UnityEngine.dll "Temp/StagingArea/TempSerializationWeaver".[Temp/StagingArea/Data/Managed\Assembly-CSharp.dll]
    Symbols will be read from Temp/StagingArea/Data/Managed/UnityEngine.pdb
    
    ...
    
    System.InvalidOperationException: Operation is not valid due to the current state of the object.
       at Mono.Cecil.MetadataImporter.ImportTypeSpecification(TypeReference type, IGenericContext context)
       at Mono.Cecil.MetadataImporter.ImportTypeSpecification(TypeReference type, IGenericContext context)
       at Unity.Serialization.Weaver.MethodEmitterBase.ResolveGenericFieldReference(FieldReference fieldRef)
       at Unity.Serialization.Weaver.MethodEmitterBase.EmitMethodBody()
       at Unity.Serialization.Weaver.SerializationWeaver.AddSerializeMethod()
       at Unity.Serialization.Weaver.SerializationWeaver.Weave()
       at usw.Weaver.WeaveAssembly(String assemblyPath, AssemblyDefinition unityEngineAssemblyDefinition)
       at usw.Weaver.Weave()
       at usw.Program.RunProgram(ConversionOptions options)
       at usw.Program.Main(String[] args)

Hi,

this looks like a bug in Serialization Weaver. Could you report it with a project attached that demonstrates this behaviour? If you do it, we could check asap and look for a work around.

Thanks - the project in question is fairly large. I’ll see if I can get a pared-down version which reproduces the issue.

I’m seeing the exact same issue:

Error building Player: Exception: Failed to run serialization weaver with command line “Temp/StagingArea/Data/Managed\Assembly-CSharp.dll” -pdb -verbose -unity-engine=Temp/StagingArea/Data/Managed/UnityEngine.dll “Temp/StagingArea/TempSerializationWeaver”.[Temp/StagingArea/Data/Managed\Assembly-CSharp.dll]
Symbols will be read from Temp/StagingArea/Data/Managed/UnityEngine.pdb
Weaving assembly C:\Users\nmcdonald\Desktop\hauntedmansion\trunk\Temp\StagingArea\Data\Managed\Assembly-CSharp.dll
Symbols will be read from Temp/StagingArea/Data/Managed\Assembly-CSharp.pdb

you’ve stripped the error.

Here is the same error on 4.5.1p1, everything works fine on 4.3.7p2

Error building Player: Exception: Failed to run serialization weaver with command line "Temp\StagingArea\Data\Managed\Assembly-CSharp.dll" -pdb -verbose -unity-engine=Temp\StagingArea\Data\Managed\UnityEngine.dll "Temp\StagingArea\TempSerializationWeaver".[Temp\StagingArea\Data\Managed\Assembly-CSharp.dll]
Symbols will be read from Temp\StagingArea\Data\Managed\UnityEngine.pdb
Weaving assembly C:\Projects\_game\tinyheroes_unity_wp8\Temp\StagingArea\Data\Managed\Assembly-CSharp.dll
Symbols will be read from Temp\StagingArea\Data\Managed\Assembly-CSharp.dll.mdb
 + ArenaBattleScript
 + ArenaFinishScreen
 + ArenaRecordLine
 + ArenaScreen
 + ArenaShop
 + ArenaShopItemSlot
 + ActionButton
 + Activatable
 + BattleEndPopup
 + BattleEndPopupSlot
 + BattleFieldCameraConfiguration
 + BattleReward
 + BattleScene
 + BattleSceneBuilder
 + BattleSceneDebugPanel
 + BattleSceneDebugScript
 + BattleSceneScript
 + BattleTutorial1
 + BattleTutorial2
 + BattleTutorial3
 + BattleTutorial4
 + BattleTutorialLogic
 + BattleTutorialMessage
 + BattleTutorialMessageArrow
 + Chest
 + CreatureCountAnimation
 + CreatureHealthBar
 + CreatureHitAnimation
 + CreatureStack
 + CreatureStackArenaEnemySlot
 + CreatureStackBuilder
 + CreatureStackBuilderBase
 + CreatureStackIndication
 + CreatureStackPlayerSlot
 + Field
 + FieldObject
 + FieldTile
 + FieldTileIndicator
 + HelpButton
 + Loot
 + Obstacle
 + Placeable
 + PlaceableTileOffset
 + Player
 + RageButton
 + RagePopup
 + RagePopupSpell
 + Script
 + SingleStackBuilder
 + ComicsScreen
 + ComicsScreen1
 + ComicsScreen2
 + ActionArea
 + ActionAreaOffset
 + ActionBase
 + ActionBuff
 + ActionBuffAll
 + ActionBuffMass
 + ActionBuffRandom
 + ActionCharge
 + ActionChest
 + ActionHeal
 + ActionMelee
 + ActionRanged
 + ActionRangedChargeAnimated
 + ActionRangedCharged
 + ActionRangedMiss
 + ActionRouter
 + ActionSpawn
 + ActionWave
 + ActionWaveSplash
 + AnimatedActionBase
 + ActionDamage
 + ActionEffect
 + CreatureAction
 + Spell
 + AllTargetSpell
 + BuffSpell
 + FireArrowSpell
 + FireRainSpell
 + InspirationSpell
 + MultiTargetSpell
 + RandomTargetSpell
 + AnimatedFader
 + AnimationBase
 + AnimationChain
 + AnimationChainEvent
 + AnimationChainEventOutput
 + AnimationChainTimeEvent
 + ColorPulseAnimation
 + CreatureAnimation
 + CreatureAnimationSoundEvent
 + CreatureStackAnimation
 + DelayAccumulatorAnimation
 + DelayAnimation
 + DestroyObjectAnimation
 + FlyOutAnimation
 + OpacityAnimation
 + ParticleAnimation
 + ParticleEmissionAnimation
 + ParticleStopAnimation
 + ProjectileAnimation
 + ProjectileBase
 + ProjectileOutOfScreenAnimation
 + ProjectileParabolaAnimation
 + ProjectileParabolaAnimation2
 + ProjectileUpDownAnimation
 + RageConfigureAnimation
 + RageCounterAnimation
 + ScalePulseAnimation
 + ShakeAnimation
 + SignalAnimation
 + SimpleSpriteAnimation
 + SoundAnimation
 + SpriteProjectile
 + TargetParticleAnimation
 + UnityBuiltinAnimation
 + UnityBuiltinAnimationHelper
 + CreatureAttributes
 + CreatureCustomAnimation
 + Creature
 + CreatureMetadata
 + AttackEffect
 + BurnEffect
 + BurstEffect
 + CreatureStackEffectBuilder
 + CreatureStackEffectVisual
 + DefenseEffect
 + EffectVisualFactory
 + EffectVisualObject
 + FearEffect
 + CreatureFeature
 + ImmovableFeature
 + NoEnemyRetaliationFeature
 + FreezeEffect
 + GolemDefenseEffect
 + InspirationEffect
 + BeltBanditEffect
 + BeltMonkEffect
 + HelmStrawHatEffect
 + ItemEffect
 + WeaponBowEffect
 + WeaponDaggerEffect
 + WeaponSandSwordEffect
 + ParticleEffectVisualObject
 + SimpleEffectVisualObject
 + SpeedEffect
 + SpriteEffectVisualObject
 + StunEffect
 + ActionOnCreatureDeath
 + CreatureScript
 + EnableCreatureSpecialAction
 + RemoveBodyOnCreatureDeath
 + SetCreatureFlag
 + UnlockCreatureSpecialAction
 + DemoEndScreen
 + GameData.SaveManager
 + Loader
 + LoaderAlawarScreen
 + LoaderMain
 + LoaderScreen
 + LoaderScreenAndroid
 + MainMenuDifficulties
 + MainMenuLanguages
 + MainMenuPanel
 + MainMenuScreen
 + MainMenuSlot
 + MainMenuSlots
 + MainMenuSoundScreen
 + ChatTest
 + Mission3_9Script
 + Mission5DemoScript
 + AlertPopup
 + Chat
 + ChatBase
 + ChatComics
 + ChatMessage
 + ChatMessageBase
 + ChatMessageEmpty
 + CreditsPopup
 + CrossPromoBannerDisplay
 + CrossPromoPopup
 + InfoPopup
 + ItemInfoPopup
 + Popup
 + PromptPopup
 + SettingsDifficultyPopup
 + SettingsPopup
 + SettingsVolumeControl
 + SettingsVolumePopup
 + Tooltip
 + UnitInfoFullPopup
 + UnitInfoShortPopup
 + AchievementArmorSpell
 + AchievementSpeedSpell
 + AdManager
 + BackgroundUVScroll
 + BundleVersion
 + CrossPromoManager
 + DestroyParticles
 + Disabler
 + FlyOut
 + HSBColor
 + HUDFPS
 + MoveBounce
 + MoveElliptic
 + MoveForward
 + MoveGravity
 + MoveHypotrochoid
 + MoveSpiral
 + MoveWave
 + Mover
 + OnlyTutorial
 + OpacityPulse
 + PanelSlider
 + PhoneOffset
 + PositionPulse
 + Rotation
 + ScalePulse
 + StatisticsManager
 + TestFlightManager
 + ArmyPopup
 + ArmyPopupUnitBig
 + ArmyPopupUnitSmall
 + BuyGamePopup
 + BattleEvent
 + CustomEvent
 + MessageEvent
 + ShopEvent
 + HeroIcon
 + HeroItemsPopup
 + HeroItemsPopupAttribute
 + HeroItemsPopupItem
 + HeroPopup
 + HeroPopupAttribute
 + HeroPopupSlot
 + Item
 + ItemAttributeBonus
 + LevelUpPopup
 + LevelUpPopupAttribute
 + Node
 + NodeEvent
 + NodeScript
 + RewardPopup
 + Road
 + RoadBezierMidpoint
 + TutorialVisualScript
 + CreatureAbilityShopItem
 + CreatureShopItem
 + PlayerItemShopItem
 + SpellShopItem
 + ShopItem
 + ShopPopup
 + ShopPopupItemSlot
 + UnitPanel
 + WorldMap
 + WorldMapCameraConfiguration
 + WorldMapDebugPanel
 + WorldMapGoldBar
 + WorldMapHero
 + WorldMapOpacityController
 + WorldMapOverlay
 + WorldMapSceneBuilder
 + WorldMapScript
 + WorldMapUnitRare
 + WorldMapView
 + WorldMapXPBar
 + MouseTest
 + PngLoadTest
 + PrefabActiveTest
 + PrefabActiveTest2
 + StartScreen
 + TestDisplays
 + TestTextureLoading
Will export symbols of pdb format
System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
   at Mono.Cecil.Mdb.MdbReader.ReadLocalVariables(MethodEntry entry, MethodSymbols symbols)
   at Mono.Cecil.Mdb.MdbReader.Read(MethodSymbols symbols)
   at Mono.Cecil.Cil.CodeReader.PatchRawMethodBody(MethodDefinition method, CodeWriter writer, MethodSymbols& symbols)
   at Mono.Cecil.Cil.CodeWriter.WriteUnresolvedMethodBody(MethodDefinition method)
   at Mono.Cecil.Cil.CodeWriter.WriteMethodBody(MethodDefinition method)
   at Mono.Cecil.MetadataBuilder.AddMethod(MethodDefinition method)
   at Mono.Cecil.MetadataBuilder.AddMethods(TypeDefinition type)
   at Mono.Cecil.MetadataBuilder.AddType(TypeDefinition type)
   at Mono.Cecil.MetadataBuilder.AddNestedTypes(TypeDefinition type)
   at Mono.Cecil.MetadataBuilder.AddType(TypeDefinition type)
   at Mono.Cecil.MetadataBuilder.AddTypeDefs()
   at Mono.Cecil.MetadataBuilder.BuildTypes()
   at Mono.Cecil.MetadataBuilder.BuildModule()
   at Mono.Cecil.ModuleWriter.<BuildMetadata>b__0(MetadataBuilder builder, MetadataReader _)
   at Mono.Cecil.ModuleDefinition.Read[TItem,TRet](TItem item, Func`3 read)
   at Mono.Cecil.ModuleWriter.WriteModuleTo(ModuleDefinition module, Stream stream, WriterParameters parameters)
   at Mono.Cecil.ModuleDefinition.Write(String fileName, WriterParameters parameters)
   at usw.Weaver.Weave()
   at usw.Program.RunProgram(ConversionOptions options)
   at usw.Program.Main(String[] args)

Hi,

looks like .mdb file somehow ends up in the temp folder. Could you try exiting unity, delete temp folder in project directory, start Unity again and try to build?

Also, could you report a bug so this doesn’t get lost and so we could actually fix it?

This doesn’t help. Unity automatically deletes Temp folder on exit and restart doesn’t help.

I’ve submitted a bug report 615659 with the project to reproduce the case attached.

Thanks, I’ll take a look at it tomorrow.

I am trying to port my games to WP8 and am having the same issue when I build. I posted a report in case they need more examples of the bug:

Case 616209

Same issue here in Unity 4.5.1, please fix!!!

Is that the whole error you get?
You get SerializationWeaver error on Assembly-CSharp.dll, which means there is something related to your C# scripts. I would help if you could narow down to a most simple possible project.

The crash “System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.” is fixed in the very next patch release, which should come out on Thursday.

@anon_43237147 : you’re not posting the whole error message, so I don’t know whether that’s the same issue.

Hi, Ive been having this same issue and ive just downlaoded and installed 4.5.2 and the issue persists, is there any workaround for this?

Hello, I had the same problem. I created a new visual studio project and ran the weaver with the argument as debugging command line. When it break for the exception, you should be able to find out what function is failing. In my case it was a debug function so I just #if !UNITY_WINRT it.

Best Regards,

Thanks for the reply but the exception occurss duing the build so I couldn’t add breakpoints.

I finally tracked down the cause though. Apparently if you have unreachable code below a yield break it causes the exception.

The following will cause the exception

public IEnumerable test()
    {
        #if UNITY_WINRT
            yield break;
        #endif

        int i = 0;       
    }
1 Like

I same error , plese help me if You fixed it !!!
Thanks

Can someone please chime in with what in the world a “Serialization Weaver” is? The forums have solutions, but nobody seems to be bothering to explain what this thing is supposed to do. That would definitely help me understand the problem.

Serialization Weaver is Unity’s patching tool, it takes your compiled assembly and adds additional code inside for the data serialization purposes. That said adding additional code at IL level is not always easy :slight_smile:

That is a very good question :).

Unity uses serialization in many places throughout the engine: anything from scene loading to object instantiation is achieved with the help of serialization. On Mono platforms, we achieve serialization by writing data directly to managed object fields with native Mono APIs (we just get a pointer to the object and write it as if it was native memory).

However, we cannot do the same on .NET due to couple of reasons:

  • We cannot get a native pointer to managed objects. There’s simply no native APIs to interact with the CLR from native code available to WinRT platforms;
  • Even if we could get a pointer, .NET implements a GC that moves memory around, so it wouldn’t be safe to do it.

So, we have couple of options. The first one (and obvious one), is to use reflection to write out the fields. We did that in Unity 4.2, however, that was so terribly slow that larger scenes took forever to load on phones. So, with 4.3, we started using Serialization Weaver for doing this job.

The main purpose of Serialization Weaver is to generate serialization code for your classes at game build time. Suppose you have such class:

internal class Foo : MonoBehaviour
{
    public int myInt;
    public GameObject targetObject;
    private RigidBody m_RigidBody;

    private void Start()
    {
        m_RigidBody = GetComponent<RigidBody>();
        m_RigidBody.position = targetObject.GetComponent<Transform>().position + new Vector3(myInt, myInt, myInt);
    }

    private void Update()
    {
        m_RigidBody.MovePosition(m_RigidBody.position + new Vector3(0.5f, 0.5f, 0.5f));
    }
}

When you build your game, serialization weaver will analyze your built scripts, will look at this class and will implement IUnitySerializable interface in it:

internal class Foo : MonoBehaviour, IUnitySerializable
{
    public void Unity_Serialize()
    {
        UnityEngine.Serialization.SerializedStateWriter.Instance.WriteInt32(myInt);
        UnityEngine.Serialization.SerializedStateWriter.Instance.WriteUnityEngineObject(targetObject);
    }

    public void Unity_Deserialize()
    {
        myInt = UnityEngine.Serialization.SerializedStateReader.Instance.ReadInt32();
        targetObject = UnityEngine.Serialization.SerializedStateReader.Instance.ReadUnityEngineObject() as GameObject;
    }

    public void Unity_RemapPPtrs()
    {
        if (targetObject != null)
        {
            targetObject = UnityEngine.Serialization.PPtrRemapper.Instance.GetNewInstanceToReplaceOldInstance(this.targetObject) as GameObject;
        }
    }
}

Serialization Weaver will generate specialized serialization code for each class that Unity might serialize - that is anything that is derived from UnityEngine.Object or marked with the System.SerializableAttribute. In result, this makes the serialization process very fast at runtime.

Now you know how serialization works on WinRT platforms today :).

By the way - the SerializationWeaver crash in question wasn’t due to SerializationWeaver bug - there was a bug in Mono compiler. We fixed it in one of the patch releases.