Skip to content

Instantly share code, notes, and snippets.

@snaka
Created July 30, 2014 11:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snaka/3fa2f284fde7c3f82903 to your computer and use it in GitHub Desktop.
Save snaka/3fa2f284fde7c3f82903 to your computer and use it in GitHub Desktop.
ソースコード修正時にファイルエンコーディングがBOM付きUTF-8かどうかをチェックする ref: http://qiita.com/snaka/items/ae01990b147570fa0e75
using UnityEditor;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.IO;
public class SourceFileEcodingChecker : AssetPostprocessor {
static Regex m_SourceFilePattern;
static SourceFileEcodingChecker() {
m_SourceFilePattern = new Regex(@".+\.cs");
}
static void OnPostprocessAllAssets(
string[] importedAssets,
string[] deletedAssets,
string[] movedAssets,
string[] movedFromAssetPaths) {
var invalidFiles = new List<string>();
foreach (var importedAsset in importedAssets) {
if (m_SourceFilePattern.IsMatch(importedAsset)) {
var dataPath = Application.dataPath;
dataPath = dataPath.Substring(0, dataPath.Length - 7);
var path = System.IO.Path.Combine(dataPath, importedAsset);
Debug.Log ("*** import: " + path);
using(FileStream fs = File.OpenRead(path)) {
var head = new byte[3];
fs.Read (head, 0, 3);
var headStr = System.BitConverter.ToString(head);
Debug.Log (headStr);
if (headStr != "EF-BB-BF") {
invalidFiles.Add(path);
}
fs.Close ();
}
}
}
if (invalidFiles.Count > 0) {
EditorUtility.DisplayDialog(
"File encoding check error",
string.Join ("\n",invalidFiles.ToArray ()),
"OK"
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment