Firstborn/Assets/Scripts/LoadSaveActors.cs
Schaken-Mods 959e80cf72 assets upload
assets upload description.
2023-03-28 12:16:30 -05:00

212 lines
8.5 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.IO;
using UnityEngine.UI;
using TMPro;
using RPGCreationKit;
namespace RPGCreationKit
{
public class LoadSaveActors : MonoBehaviour
{
[SerializeField] private string Folder;
[SerializeField] private TMP_InputField AvatarName;
[SerializeField] private CharacterCreationManager CCM;
[SerializeField] private GameObject ForgotNamePanel;
[SerializeField] private GameObject MessagePanel;
[SerializeField] private GameObject AvatarPrefab;
[SerializeField] private Transform AvatarPrefabContainer;
[SerializeField] private List<GameObject> CreatedAvatarPrefab;
[SerializeField] private GameObject ME;
[SerializeField] private Toggle MakeMale;
[SerializeField] private Toggle MakeFemale;
// Start is called before the first frame update
void Start()
{
Folder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)+@"\My Games\Firstborn\Avatars";
if (!Directory.Exists(Folder)) {
Directory.CreateDirectory(Folder);
}
LoadAvatarList();
}
// Update is called once per frame
void Update()
{
}
public void SaveAvatar() {
SkinnedMeshRenderer Face = CCM.currentCharacter.head; // this needs found
// Display all blendshapes
bool FoundEnd = false;
if (AvatarName.text.Length <= 0) {
MessagePanel.SetActive(true);
ForgotNamePanel.SetActive(true);
return;
}
using (StreamWriter sw = File.CreateText(Folder+@"\"+AvatarName.text+".ini")) {
sw.WriteLine("[AvatarInfo]");
Race race = CCM.selectedRace;
sw.WriteLine("RaceName="+race.raceName+";");
bool isMale = CCM.isCreatingMale;
sw.WriteLine("IsMale="+isMale+";");
if (isMale == true) {
sw.WriteLine("HairID="+race.maleHairTypes[CCM.selectedHair].ID+";");
} else {
sw.WriteLine("HairID="+race.femaleHairTypes[CCM.selectedHair].ID+";");
}
sw.WriteLine("EyesID="+race.eyeTypes[CCM.selectedEyes].ID+";");
sw.WriteLine("SkinColor="+CCM.SkinColor+";");
sw.WriteLine("HairColor="+CCM.HairColor+";");
sw.WriteLine("LipsColor="+CCM.LipsColor+";");
/// Blendshape Madness!/////
sw.WriteLine("[Blendshapes]");
for (int i = 0; ((i < Face.sharedMesh.blendShapeCount) && (FoundEnd == false)); i++)
{
if (Face.sharedMesh.GetBlendShapeName(i).ToString().Contains("===")) {
FoundEnd = true;
} else {
sw.WriteLine(Face.sharedMesh.GetBlendShapeName(i).ToString()+"="+Face.GetBlendShapeWeight(i)+";");
}
}
}
LoadAvatarList();
}
public void LoadAvatarList() {
foreach (Transform T in CCM.headBlendshapeContent)
Destroy(T.gameObject);
foreach (Transform T in AvatarPrefabContainer)
Destroy(T.gameObject);
DirectoryInfo dir = new DirectoryInfo(Folder);
FileInfo[] info = dir.GetFiles("*.ini*");
int i = 0;
for (int j = 0; j < CreatedAvatarPrefab.Count; j++) {
CreatedAvatarPrefab[j].SetActive(false);
}
CreatedAvatarPrefab.Clear();
foreach (FileInfo f in info) {
var NewAvatar = Instantiate(AvatarPrefab, AvatarPrefabContainer);
CreatedAvatarPrefab.Add(NewAvatar);
AvatarFilePrefab NewAvatarGO = NewAvatar.GetComponent<AvatarFilePrefab>();
NewAvatarGO.Name.text = f.Name.Split(new string[] { @".ini" }, StringSplitOptions.None)[0].Trim ();
using (var sr = new StreamReader(Folder+@"\"+f.Name))
{
string A = sr.ReadToEnd();
NewAvatarGO.Race.text = A.Split(new string[] { @"RaceName=" }, StringSplitOptions.None)[1].Split(";")[0].Trim ();
string MF = A.Split(new string[] { @"IsMale=" }, StringSplitOptions.None)[1].Split(";")[0].Trim ();
if (MF.Contains("True")) {
MF = "Male";
} else {
MF = "Female";
}
NewAvatarGO.Sex.text = MF;
NewAvatarGO.FilePath = f.FullName;
LoadSaveActors MEGO = ME.GetComponent<LoadSaveActors>();
NewAvatarGO.LSAGO = MEGO;
}
i += 1;
}
}
public void LoadAvatar(string FP) {
using (var sr = new StreamReader(FP)) {
Race race = CCM.selectedRace;
string A = sr.ReadToEnd();
string RaceName = A.Split(new string[] { @"RaceName=" }, StringSplitOptions.None)[1].Split (";")[0].Trim ();
string MF = A.Split(new string[] { @"IsMale=" }, StringSplitOptions.None)[1].Split (";")[0].Trim ();
string HairType = A.Split(new string[] { @"HairID=" }, StringSplitOptions.None)[1].Split (";")[0].Trim ();
string EyesType = A.Split(new string[] { @"EyesID=" }, StringSplitOptions.None)[1].Split (";")[0].Trim ();
string HairColor = A.Split(new string[] { @"HairColor=" }, StringSplitOptions.None)[1].Split (";")[0].Trim ();
string LipsColor = A.Split(new string[] { @"LipsColor=" }, StringSplitOptions.None)[1].Split (";")[0].Trim ();
string SkinColor = A.Split(new string[] { @"SkinColor=" }, StringSplitOptions.None)[1].Split (";")[0].Trim ();
AvatarName.text = FP.Split(new string[] { @"\Avatars\" }, StringSplitOptions.None)[1].Split (new string[] { @".ini" }, StringSplitOptions.None)[0].Trim ();
string Blendshapes = A.Split (new string[] { @"Blendshapes" }, StringSplitOptions.None)[1].Trim ();
// Set the race
bool Found = false;
for (int i = 0; ((i < CCM.playableRaces.Length) && (Found == false)); i++) {
if (CCM.playableRaces[i].raceName == RaceName) {
Found = true;
CCM.selectedRace = CCM.playableRaces[i];
}
}
// Set the Sex
bool DidChangeSex = false;
bool isMale = false;
if (MF.Contains("True")) {
if (MakeMale.isOn == false) {
MakeMale.isOn = true;
MakeFemale.isOn = false;
DidChangeSex = true;
isMale = true;
}
} else {
if (MakeMale.isOn == true) {
MakeMale.isOn = false;
MakeFemale.isOn = true;
DidChangeSex = true;
}
}
if (DidChangeSex == false) { // if it is the same sex, the race wont change, so lets change it here.
CCM.LoadRaceCharacter(CCM.selectedRace);
}
// Set the hair
if (isMale == true) {
for (int i = 0; i < race.maleHairTypes.Count; i++) {
if (race.maleHairTypes[i].ID == HairType) {
CCM.selectedHair = i;
}
}
} else {
for (int i = 0; i < race.femaleHairTypes.Count; i++) {
if (race.femaleHairTypes[i].ID == HairType) {
CCM.selectedHair = i;
}
}
}
CCM.OnHairChanges();
for (int i = 0; i < race.eyeTypes.Count; i++) {
if (race.eyeTypes[i].ID == HairType) {
CCM.selectedEyes = i;
}
}
CCM.OnEyesChanges();
CCM.OnChangeHairColor(GetColor(HairColor));
CCM.OnChangeSkinColor(GetColor(SkinColor));
CCM.OnChangeLipsColor(GetColor(LipsColor));
// Set the Blendshapes
SkinnedMeshRenderer Face = CCM.currentCharacter.head;
string[] Content = Blendshapes.Split("=");
for (int i = 1; ((i < Content.Length) && (i < Face.sharedMesh.blendShapeCount)); i++) {
string TempString = Content[i].Split(";")[0].Trim();
Face.SetBlendShapeWeight(i, int.Parse(TempString));
}
CCM.currentCharacter.head.GetComponent<HeadBlendshapesManager>().AdjustChildBlendshapes();
}
}
private Color GetColor(string A) {
string[] rgba = A.Substring(5, A.Length - 6).Split(", ");
return new Color(float.Parse(rgba[0]), float.Parse(rgba[1]), float.Parse(rgba[2]), float.Parse(rgba[3]));
}
}
}