Skip to content

Commit

Permalink
Merge pull request #1 from jbengtson/master
Browse files Browse the repository at this point in the history
1.1 update
  • Loading branch information
NathanKell committed Apr 23, 2016
2 parents 9bfadb2 + 9df6cab commit 9f305da
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 67 deletions.
Binary file added KerbalRenamer.1.1.zip
Binary file not shown.
Binary file modified KerbalRenamer/GameData/KerbalRenamer/plugins/Renamer.dll
Binary file not shown.
2 changes: 1 addition & 1 deletion KerbalRenamer/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2014~2015, Justin Bengtson
Copyright (c) 2014~2016, Justin Bengtson
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
Expand Down
187 changes: 125 additions & 62 deletions Renamer/Renamer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2014~2015, Justin Bengtson
Copyright (c) 2014~2016, Justin Bengtson
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
Expand Down Expand Up @@ -30,38 +30,44 @@
using System.IO;
using System.Reflection;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using System.Linq;

namespace regexKSP {
[KSPAddon(KSPAddon.Startup.MainMenu, true)]
[KSPAddon(KSPAddon.Startup.SpaceCentre, true)]
public class KerbalRenamer : MonoBehaviour {
void Start() {
ConfigNode data = null;
foreach(ConfigNode node in GameDatabase.Instance.GetConfigNodes("KERBALRENAMER")) {
data = node;
}
if(data == null) { return; }

// We use this method so that we're not constantly loading node from the GameDatabase.
KerbalRenamerData.instance = new KerbalRenamerData(data);
}
}

public class KerbalRenamerData {
public static KerbalRenamerData instance;

public static KerbalRenamer rInstance = null;
private float badassPercent = 0.05f;
private float femalePercent = 0.5f;
private bool useBellCurveMethod = true;
private bool dontInsultMe = false;
private bool preserveOriginals = false;
private bool generateNewStats = true;
// new
private string cultureDescriptor = "Culture";
// end new
public string cultureDescriptor = "Culture";
private Culture[] cultures = {};

public KerbalRenamerData(ConfigNode data) {
public static KerbalRenamer Instance {
get {
if((object)rInstance == null) {
rInstance = (new GameObject("RenamerContainer")).AddComponent<KerbalRenamer>();
}
return rInstance;
}
}

public void Awake() {
DontDestroyOnLoad(this);

ConfigNode data = null;
foreach(ConfigNode node in GameDatabase.Instance.GetConfigNodes("KERBALRENAMER")) {
data = node;
}
if((object)data == null) {
Debug.Log("KerbalRenamer: No config file found, thanks for playing.");
return;
}

List<Culture> ctemp = new List<Culture>();
if(data.HasValue("badassPercent")) {
float ftemp = 0.0f;
Expand Down Expand Up @@ -99,11 +105,9 @@ public class KerbalRenamerData {
generateNewStats = btemp;
}
}
// new
if(data.HasValue("cultureDescriptor")) {
cultureDescriptor = data.GetValue("cultureDescriptor");
}
// end new
ConfigNode[] cultureclub = data.GetNodes("Culture");
for(int i = 0; i < cultureclub.Length; i++) {
Culture c = new Culture(cultureclub[i]);
Expand All @@ -112,7 +116,6 @@ public class KerbalRenamerData {
cultures = ctemp.ToArray();

GameEvents.onKerbalAdded.Add(new EventData<ProtoCrewMember>.OnEvent(OnKerbalAdded));
GameEvents.onGUIAstronautComplexSpawn.Add(new EventVoid.OnEvent(OnGUIAstronautComplexSpawn));
}

public void OnKerbalAdded(ProtoCrewMember kerbal) {
Expand Down Expand Up @@ -202,18 +205,14 @@ public class KerbalRenamerData {
}
if(lastName.Length > 0) {
if(firstName.Length > 0) {
// new
if(parent.cultureName.Length > 0) {
c.flightLog.AddEntryUnique(new FlightLog.Entry(0, cultureDescriptor, parent.cultureName));
}
// end new
return firstName + " " + lastName;
} else {
// new
if(parent.cultureName.Length > 0) {
c.flightLog.AddEntryUnique(new FlightLog.Entry(0, cultureDescriptor, parent.cultureName));
}
// end new
return lastName;
}
} else {
Expand All @@ -222,38 +221,7 @@ public class KerbalRenamerData {
}
}

// new
public void OnGUIAstronautComplexSpawn() {
CrewItemContainer cic;
CMScrollList scroll;

UnityEngine.Object[] objs = Resources.FindObjectsOfTypeAll(typeof(CMAstronautComplex));
if(objs.Length < 1) { return; }
CMAstronautComplex complex = (CMAstronautComplex) objs[0];
FieldInfo[] scrolls = typeof(CMAstronautComplex).GetFields(BindingFlags.NonPublic | BindingFlags.Instance).Where(c => c.FieldType == typeof(CMScrollList)).ToArray();

for(int i = 0; i < scrolls.Length; i++) {
scroll = (CMScrollList) scrolls[i].GetValue(complex);
for(int j = 0; j < ((UIScrollList) scroll).Count; j++) {
UIListItemContainer listItemContainer = (UIListItemContainer) ((UIScrollList) scroll).GetItem(j);
cic = (CrewItemContainer) listItemContainer.GetComponent<CrewItemContainer>();
FlightLog.Entry flight = cic.GetCrewRef().flightLog.Entries.FirstOrDefault(e => e.type == cultureDescriptor);
if(flight != null) {
FieldInfo fi = typeof(CrewItemContainer).GetFields(BindingFlags.NonPublic | BindingFlags.Instance).FirstOrDefault(c => c.FieldType == typeof(Renderer));
Debug.Log("KerbalRenamer: Cast to Renderer");
Renderer foo = (Renderer) fi.GetValue(cic);
Culture culture = getCultureByName(flight.target);
if(culture != null) {
Debug.Log("KerbalRenamer: Cast to Texture");
foo.material.mainTexture = (Texture) culture.cultureTex;
}
}
}
}
}
// end new

private Culture getCultureByName(string name) {
public Culture getCultureByName(string name) {
for(int i = 0; i < cultures.Length; i++) {
if(cultures[i].cultureName == name) {
return cultures[i];
Expand Down Expand Up @@ -290,10 +258,106 @@ public class KerbalRenamerData {
}
}

[KSPAddon(KSPAddon.Startup.SpaceCentre, false)]
public class IconChanger_SpaceCentre : IconChanger {
public void Awake() {
GameEvents.onGUIAstronautComplexSpawn.Add(new EventVoid.OnEvent(OnGUIAstronautComplexSpawn));
GameEvents.onGUILaunchScreenVesselSelected.Add(new EventData<ShipTemplate>.OnEvent(OnGUILaunchScreenVesselSelected));
}
}

[KSPAddon(KSPAddon.Startup.EditorAny, false)]
public class IconChanger_EditorAny : IconChanger {
public void Awake() {
GameEvents.onEditorScreenChange.Add(new EventData<EditorScreen>.OnEvent(OnEditorScreenChange));
}
}

public class IconChanger : MonoBehaviour {
public void OnGUIAstronautComplexSpawn() {
StartCoroutine(CallbackUtil.DelayedCallback(1, BuildAstronautComplex));
}

public void OnGUILaunchScreenVesselSelected(ShipTemplate t) {
StartCoroutine(CallbackUtil.DelayedCallback(1, BuildCrewAssignmentDialogue));
}

public void OnEditorScreenChange(EditorScreen e) {
if(e == EditorScreen.Crew) {
StartCoroutine(CallbackUtil.DelayedCallback(1, BuildCrewAssignmentDialogue));
}
}

public void BuildAstronautComplex() {
KSP.UI.CrewListItem cic;
KSP.UI.UIList scroll;

UnityEngine.Object[] objs = Resources.FindObjectsOfTypeAll(typeof(KSP.UI.Screens.AstronautComplex));
if(objs.Length < 1) { return; }
KSP.UI.Screens.AstronautComplex complex = (KSP.UI.Screens.AstronautComplex) objs[0];
FieldInfo[] scrolls = typeof(KSP.UI.Screens.AstronautComplex).GetFields(BindingFlags.NonPublic | BindingFlags.Instance).Where(c => c.FieldType == typeof(KSP.UI.UIList)).ToArray();

for(int i = 0; i < scrolls.Length; i++) {
scroll = (KSP.UI.UIList) scrolls[i].GetValue(complex);
for(int j = 0; j < scroll.Count; j++) {
KSP.UI.UIListItem listItem = scroll.GetUilistItemAt(j);
cic = listItem.GetComponent<KSP.UI.CrewListItem>();
cic.AddButtonInputDelegate(new UnityAction<KSP.UI.CrewListItem.ButtonTypes, KSP.UI.CrewListItem>(RebuildAstronautComplex));
changeKerbalIcon(cic);
}
}
}

public void BuildCrewAssignmentDialogue() {
if((object) KSP.UI.CrewAssignmentDialog.Instance == null) {
return;
}
KSP.UI.CrewAssignmentDialog dialogue = KSP.UI.CrewAssignmentDialog.Instance;
KSP.UI.CrewListItem cic;

for(int j = 0; j < dialogue.scrollListAvail.Count; j++) {
KSP.UI.UIListItem listItem = dialogue.scrollListAvail.GetUilistItemAt(j);
cic = listItem.GetComponent<KSP.UI.CrewListItem>();
cic.AddButtonInputDelegate(new UnityAction<KSP.UI.CrewListItem.ButtonTypes, KSP.UI.CrewListItem>(RebuildCrewAssignmentDialogue));
changeKerbalIcon(cic);
}
for(int j = 0; j < dialogue.scrollListCrew.Count; j++) {
KSP.UI.UIListItem listItem = dialogue.scrollListCrew.GetUilistItemAt(j);
cic = listItem.GetComponent<KSP.UI.CrewListItem>();
if((object)cic != null) {
cic.AddButtonInputDelegate(new UnityAction<KSP.UI.CrewListItem.ButtonTypes, KSP.UI.CrewListItem>(RebuildCrewAssignmentDialogue));
changeKerbalIcon(cic);
}
}
}

public void RebuildAstronautComplex(KSP.UI.CrewListItem.ButtonTypes type, KSP.UI.CrewListItem cic) {
StartCoroutine(CallbackUtil.DelayedCallback(1, BuildAstronautComplex));
}

public void RebuildCrewAssignmentDialogue(KSP.UI.CrewListItem.ButtonTypes type, KSP.UI.CrewListItem cic) {
StartCoroutine(CallbackUtil.DelayedCallback(1, BuildCrewAssignmentDialogue));
}

private void changeKerbalIcon(KSP.UI.CrewListItem cic) {
if((object)cic.GetCrewRef() == null) {
return;
}
FlightLog.Entry flight = cic.GetCrewRef().flightLog.Entries.FirstOrDefault(e => e.type == KerbalRenamer.Instance.cultureDescriptor);
if((object)flight != null) {
FieldInfo fi = typeof(KSP.UI.CrewListItem).GetFields(BindingFlags.NonPublic | BindingFlags.Instance).FirstOrDefault(c => c.FieldType == typeof(RawImage));
RawImage foo = (RawImage) fi.GetValue(cic);
Culture culture = KerbalRenamer.Instance.getCultureByName(flight.target);
if((object)culture != null) {
foo.texture = (Texture) GameDatabase.Instance.GetTexture("KerbalRenamer/Icons/" + culture.cultureName, false);
}
}
}
}

public class Culture {
public bool femaleSurnamesExist = false;
public string cultureName = "";
public Texture cultureTex = null;
public string[] fnames1 = {};
public string[] fnames2 = {};
public string[] fnames3 = {};
Expand All @@ -312,7 +376,6 @@ public class Culture {
string[] vals;
if(node.HasValue("name")) {
cultureName = node.GetValue("name");
cultureTex = GameDatabase.Instance.GetTexture("KerbalRenamer/Icons/" + cultureName, false);
}
if(node.HasNode("FFIRSTNAME1")) {
temp = node.GetNode("FFIRSTNAME1");
Expand Down
17 changes: 13 additions & 4 deletions Renamer/Renamer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,22 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="Assembly-CSharp">
<HintPath>..\..\..\..\.local\share\Steam\SteamApps\common\Kerbal Space Program\KSP_Data\Managed\Assembly-CSharp.dll</HintPath>
</Reference>
<Reference Include="UnityEngine">
<HintPath>..\..\..\..\.local\share\Steam\SteamApps\common\Kerbal Space Program\KSP_Data\Managed\UnityEngine.dll</HintPath>
<HintPath>..\..\..\..\..\.local\share\Steam\SteamApps\common\Kerbal Space Program\KSP_Data\Managed\UnityEngine.dll</HintPath>
</Reference>
<Reference Include="System.Core" />
<Reference Include="UnityEngine.UI">
<HintPath>..\..\..\..\..\.local\share\Steam\SteamApps\common\Kerbal Space Program\KSP_Data\Managed\UnityEngine.UI.dll</HintPath>
</Reference>
<Reference Include="KSPCore">
<HintPath>..\..\..\..\..\.local\share\Steam\SteamApps\common\Kerbal Space Program\KSP_Data\Managed\KSPCore.dll</HintPath>
</Reference>
<Reference Include="KSPUtil">
<HintPath>..\..\..\..\..\.local\share\Steam\SteamApps\common\Kerbal Space Program\KSP_Data\Managed\KSPUtil.dll</HintPath>
</Reference>
<Reference Include="Assembly-CSharp">
<HintPath>..\..\..\..\..\.local\share\Steam\SteamApps\common\Kerbal Space Program\KSP_Data\Managed\Assembly-CSharp.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down

0 comments on commit 9f305da

Please sign in to comment.