summaryrefslogtreecommitdiff
path: root/Thronefall_1_57/Decompile/I2.Loc/TranslationJob_GET.cs
blob: d22440123092c0c57c0c1f8debbbdfc947822195 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using System.Collections.Generic;
using System.Text;
using UnityEngine.Networking;

namespace I2.Loc;

public class TranslationJob_GET : TranslationJob_WWW
{
	private Dictionary<string, TranslationQuery> _requests;

	private GoogleTranslation.fnOnTranslationReady _OnTranslationReady;

	private List<string> mQueries;

	public string mErrorMessage;

	public TranslationJob_GET(Dictionary<string, TranslationQuery> requests, GoogleTranslation.fnOnTranslationReady OnTranslationReady)
	{
		_requests = requests;
		_OnTranslationReady = OnTranslationReady;
		mQueries = GoogleTranslation.ConvertTranslationRequest(requests, encodeGET: true);
		GetState();
	}

	private void ExecuteNextQuery()
	{
		if (mQueries.Count == 0)
		{
			mJobState = eJobState.Succeeded;
			return;
		}
		int index = mQueries.Count - 1;
		string text = mQueries[index];
		mQueries.RemoveAt(index);
		string uri = LocalizationManager.GetWebServiceURL() + "?action=Translate&list=" + text;
		www = UnityWebRequest.Get(uri);
		I2Utils.SendWebRequest(www);
	}

	public override eJobState GetState()
	{
		if (www != null && www.isDone)
		{
			ProcessResult(www.downloadHandler.data, www.error);
			www.Dispose();
			www = null;
		}
		if (www == null)
		{
			ExecuteNextQuery();
		}
		return mJobState;
	}

	public void ProcessResult(byte[] bytes, string errorMsg)
	{
		if (string.IsNullOrEmpty(errorMsg))
		{
			errorMsg = GoogleTranslation.ParseTranslationResult(Encoding.UTF8.GetString(bytes, 0, bytes.Length), _requests);
			if (string.IsNullOrEmpty(errorMsg))
			{
				if (_OnTranslationReady != null)
				{
					_OnTranslationReady(_requests, null);
				}
				return;
			}
		}
		mJobState = eJobState.Failed;
		mErrorMessage = errorMsg;
	}
}