<commit_before>//
//  ShaderDocument.swift
//  MetalToy
//
//  Created by minimoog on 1/22/18.
//  Copyright © 2018 <NAME>. All rights reserved.
//

import UIKit

class ShaderDocument: UIDocument {
    var shaderInfo: ShaderInfo?
    var thumbnail: UIImage?
    
    enum SubDocumentType: String {
        case shaderinfo = "shaderinfo.json"
        case thumbnail = "thumbnail.png"
        case shader = "shader.txt"  //old format
        case name = "name.txt"      //old format
    }
    
    override init(fileURL url: URL) {
        super.init(fileURL: url)
    }
    
    init() {
        let tempDir = FileManager.default.temporaryDirectory
        let url = tempDir.appendingPathComponent("MyShader.shader")
        
        super.init(fileURL: url)
    }
    
    override func contents(forType typeName: String) throws -> Any {
        if let shaderInfo = shaderInfo, let thumbnail = thumbnail {
            guard let jsonData = encodeToJsonData(shaderInfo: shaderInfo) else { return Data() }
            
            let shaderInfoFileWrapper = FileWrapper(regularFileWithContents: jsonData)
            
            guard let imageData = UIImagePNGRepresentation(thumbnail) else { return Data() }
            let thumbnailFileWrapper = FileWrapper(regularFileWithContents: imageData)
            
            let dirWrapper = FileWrapper(directoryWithFileWrappers: [SubDocumentType.shaderinfo.rawValue: shaderInfoFileWrapper,
                                                                     SubDocumentType.thumbnail.rawValue: thumbnailFileWrapper])
            return dirWrapper
        } else {
            return Data()
        }
    }
    
    override func load(fromContents contents: Any, ofType typeName: String?) throws {
        if let userContents = contents as? FileWrapper {
            if userContents.isDirectory {
                if  let dirWrapper = userContents.fileWrappers,
                    let shaderInfoFileWrapper = dirWrapper[SubDocumentType.shaderinfo.rawValue],
                    let thumbnailFileWrapper = dirWrapper[SubDocumentType.thumbnail.rawValue],
                    let shaderInfoData = shaderInfoFileWrapper.regularFileContents,
                    let imageData = thumbnailFileWrapper.regularFileContents {
                        shaderInfo = decodeFromJsonData(data: shaderInfoData)
                        thumbnail = UIImage(data: imageData)
                } else {
                    // read from old format
                    if let dirWrapper = userContents.fileWrappers,
                        let shaderTextFileWrapper = dirWrapper[SubDocumentType.shader.rawValue],
                        let nameFileWrapper = dirWrapper[SubDocumentType.name.rawValue],
                        let shaderTextData = shaderTextFileWrapper.regularFileContents,
                        let thumbnailFileWrapper = dirWrapper[SubDocumentType.thumbnail.rawValue],
                        let imageData = thumbnailFileWrapper.regularFileContents,
                        let nameData = nameFileWrapper.regularFileContents {
                        
                        shaderInfo = ShaderInfo(name: String(data: nameData, encoding: .utf8)!,
                                                             fragment: String(data: shaderTextData, encoding: .utf8)!,
                                                             textures: [String]())
                        
                        //remove old data
                        userContents.removeFileWrapper(nameFileWrapper)
                        userContents.removeFileWrapper(shaderTextFileWrapper)
                        
                        thumbnail = UIImage(data: imageData)
                    }
                }
            }
        }
    }
}<commit_msg>Simplify shader document.<commit_after>//
//  ShaderDocument.swift
//  MetalToy
//
//  Created by minimoog on 1/22/18.
//  Copyright © 2018 <NAME>. All rights reserved.
//

import UIKit

class ShaderDocument: UIDocument {
    var shaderInfo: ShaderInfo?
    
    override init(fileURL url: URL) {
        super.init(fileURL: url)
    }
    
    init() {
        let tempDir = FileManager.default.temporaryDirectory
        let url = tempDir.appendingPathComponent("MyShader.shader")
        
        super.init(fileURL: url)
    }
    
    override func contents(forType typeName: String) throws -> Any {
        if let shaderInfo = shaderInfo {
            guard let jsonData = encodeToJsonData(shaderInfo: shaderInfo) else { return Data() }
            
            return jsonData as Any
        }
        
        return Data()
    }
    
    override func load(fromContents contents: Any, ofType typeName: String?) throws {
        guard let data = contents as? Data else {
            fatalError("\(contents) is not an instance of Data")
        }
        
        shaderInfo = decodeFromJsonData(data: data)
    }
}
{'fraction_non_alphanumeric': 0.07241942064463484, 'fraction_numerical': 0.004079967360261118, 'mean_word_length': 1.7133370226895406, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4305813', 'n_tokens_mistral': 1233, 'n_tokens_neox': 1177, 'n_words': 287}