Reference
  Util\CommonDialog.txt
End Reference

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'LoadDataAsync
'
'Load data from the slot with running other threads.
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Procedure LoadDataAsync(slot, typeList)

  Dim asyncLoading = CreateList()
  For i = 1 To typeList.Count
    Dim type = typeList[i]
    asyncLoading.Add(Async.LoadData(slot, type, type + ".ini"))
  Next

  Dim asyncResult = Nothing
  Do
    If WaitForCompletion(asyncLoading[1], asyncResult) Then
      asyncLoading.RemoveAt(1)
    End If
    Sleep(0)
  Loop While asyncLoading.Count > 0
  Return asyncResult

End Procedure

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'LoadDataSync
'
'Load data from the slot.
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Procedure LoadDataSync(slot, typeList)

  Dim result = Nothing
  For i = 1 To typeList.Count
    Dim type = typeList[i]
    result = LoadData(slot, type, type + ".ini")
    Sleep(0)
  Next
  Return result

End Procedure

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'LoadGameData
'
'Load all data from the slot.
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Procedure LoadGameData(slot)

  If slot > 0 Then
    LoadDataSync(slot, {"Vehicle"})
    LoadDataSync(slot, {"Hero"})
    LoadDataSync(slot, {"Party"})
  Else
    LoadDataSync(0, {"Texture", "WaveBank", "Shader"})
    LoadDataAsync(0, {"Area"})
    LoadDataSync(0, {"Magic", "Tool", "GrowthInfo"})
    LoadDataSync(0, {"Actor"})
    LoadDataSync(0, {"Vehicle", "Hero", "Enemy"})
    LoadDataSync(0, {"Party"})
  End If

End Procedure

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'SaveDataAsync
'
'Save data from the slot with running other threads.
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Procedure SaveDataAsync(slot, typeList)

  Dim asyncLoading = CreateList()
  For i = 1 To typeList.Count
    Dim type = typeList[i]
    asyncLoading.Add(Async.SaveData(slot, type, type + ".ini"))
  Next

  Dim asyncResult = Nothing
  Do
    If WaitForCompletion(asyncLoading[1], asyncResult) Then
      asyncLoading.RemoveAt(1)
    End If
    Sleep(0)
  Loop While asyncLoading.Count > 0
  Return asyncResult

End Procedure

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'SaveDataSync
'
'Save data from the slot.
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Procedure SaveDataSync(slot, typeList)

  Dim result = Nothing
  For i = 1 To typeList.Count
    Dim type = typeList[i]
    result = SaveData(slot, type, type + ".ini")
    Sleep(0)
  Next
  Return result

End Procedure

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'SaveGameData
'
'Save all data to the slot.
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Procedure SaveGameData(slot)

  LoadDataSync(slot, {"SaveDataHeader"})
  Dim sdh = [SaveDataHeader]  
  Dim pm = [Party].Primary
  
  sdh.Time = System.DateTime.Now.Ticks
  sdh.AreaName = pm.Name + " LV " + pm.PPLevel + ":" + pm.MPLevel + " HP " + pm.HP + "/" + pm.MaxHP

  SaveDataSync(slot, {"SaveDataHeader"})
  SaveDataSync(slot, {"Vehicle"})
  SaveDataSync(slot, {"Hero"})
  SaveDataSync(slot, {"Party"})

End Procedure

Procedure CreateDataSlotMenuForLoad(x, y, width, height, scProc, scParam, _
  caption, index)
  Dim names = CreateArrayList(11)
  Dim dates = CreateArrayList(11)
  For slot = 1 To 10
    Dim slot_index = slot + 1
    LoadDataSync(slot, {"SaveDataHeader"})
    Dim sdh = [SaveDataHeader]
    If sdh.Time > 0L Then
      names[slot_index] = sdh.AreaName
      dates[slot_index] = CreateInstance(System.DateTime, sdh.Time).ToString("yyyy'-'MM'-'dd' 'HH':'mm")
    Else
      names[slot_index] = "データがありません"
      dates[slot_index] = ""
    End If
  Next
  names[1] = "はじめから"
  dates[1] = ""
  Return CreateMenu(x, y, width, height, scProc, scParam, caption, _
    names, dates, index)
End Procedure

Procedure CreateDataSlotMenuForSave(x, y, width, height, scProc, scParam, _
  caption, index)
  Dim names = CreateArrayList(10)
  Dim dates = CreateArrayList(10)
  For slot = 1 To 10
    Dim slot_index = slot
    LoadDataSync(slot, {"SaveDataHeader"})
    Dim sdh = [SaveDataHeader]
    If sdh.Time > 0L Then
      names[slot_index] = sdh.AreaName
      dates[slot_index] = CreateInstance(System.DateTime, sdh.Time).ToString("yyyy'-'MM'-'dd' 'HH':'mm")
    Else
      names[slot_index] = "データがありません"
      dates[slot_index] = ""
    End If
  Next
  Return CreateMenu(x, y, width, height, scProc, scParam, caption, _
    names, dates, index)
End Procedure

Procedure IsDataSlotUsed(slot)
  If 1 <= slot AndAlso slot <= 10 Then
    LoadDataSync(slot, {"SaveDataHeader"})
    Return [SaveDataHeader].Time > 0L
  Else
    Return False
  End If
End Procedure
